python系统错误_Python模拟操作系统错误异常

尝试在python3.6中使用side_neffect模拟PermissionError异常。似乎调用了我的函数并引发了EPERM异常,但之后它无法运行except语句。与“real”OSError异常的预期运行相同的代码。我的代码:#my_module.py

import os

import errno

import sys

import inspect

def open_file(fname):

try:

with open('./' + fname, 'w') as f:

print('never get here')

return(0)

except PermissionError as e:

print('ERROR: \nIn function: ' + inspect.stack()[0][3])

print('On line: {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)

sys.exit(1)

我的测试:

^{pr2}$

当我跑步时:C:\Users\mylib>coverage3 run -m unittest OpenFileMockTestCase.py -v

test_2_open_file_mock_oserror (OpenFileMockTestCase.OpenFileMockTestCase) ... starting open_file with testfile2.txt...

ERROR

======================================================================

ERROR: test_2_open_file_mock_oserror (OpenFileMockTestCase.OpenFileMockTestCase)

----------------------------------------------------------------------

Traceback (most recent call last):

File "c:\users\xti027\appdata\local\programs\python\python36\lib\unittest\mock.py", line 1179, in patched

return func(*args, **keywargs)

File "C:\Users\xti027\Documents\DataTool-Git\DataTool\DataLoaderConfig\OpenFileMockTestCase.py", line 14, in test_2_open_file_mock_oserror

mock_oserror.my_module.open_file('testfile2.txt')

File "c:\users\xti027\appdata\local\programs\python\python36\lib\unittest\mock.py", line 939, in __call__

return _mock_self._mock_call(*args, **kwargs)

File "c:\users\xti027\appdata\local\programs\python\python36\lib\unittest\mock.py", line 995, in _mock_call

raise effect

PermissionError: [Errno 1] Not Allowed

----------------------------------------------------------------------

Ran 1 test in 0.031s

FAILED (errors=1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值