关于单元测试:Python – 模拟操作系统错误异常

Python - Mocking OS Error Exception

尝试使用side_effect在Python 3.6中模拟PermissionError异常。 看起来我的函数被调用并且引发了EPERM异常,但是它无法运行我的except语句。 对于"真正的"OSError异常,相同的代码按预期运行。 我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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:
In function: '
+ inspect.stack()[0][3])
        print('On line: {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
        sys.exit(1)

我的测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#OpenFileMockTestCase.py
from unittest import TestCase
from unittest import mock
import errno
import my_module

class OpenFileMockTestCase(TestCase):

    @mock.patch('my_module.os.open')
    def test_2_open_file_mock_oserror(self, mock_oserror):
        with self.assertRaises(SystemExit):
            mock_oserror.my_module.open_file.side_effect = (OSError((errno.EPERM), 'Not Allowed'))
            print('starting open_file with testfile2.txt...')
            mock_oserror.my_module.open_file('testfile2.txt')

当我跑:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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)

我已经阅读了几个关于异常和模拟的SO问题和回答,比如我如何为OSError编写单元测试? 并查看了Python文档:https://docs.python.org/3.6/library/unittest.mock.html#module-unittest.mock
我在正确的地方嘲笑合适的物品吗?


你可以提出PermissionError异常:

1
mock_oserror.side_effect = PermissionError

请注意,我们直接在模拟open()调用上设置副作用! 我还会在被测模块中模拟全局open()名称,而不是os.open

您还应该直接调用函数测试,而不是作为mock_oserror对象的属性:

1
2
3
4
5
6
7
8
9
10
import my_module

# ....

@mock.patch('my_module.open')
def test_2_open_file_mock_oserror(self, mock_open):
    mock_open.side_effect = PermissionError
    print('starting open_file with testfile2.txt...')
    with self.assertRaises(SystemExit):
        my_module.open_file('testfile2.txt')

我在这里使用了名称mock_open,因为它更好地反映了被嘲笑的内容。

演示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
>>> import os
>>> import errno
>>> import sys
>>> import inspect
>>> from unittest import mock
>>> def open_file(fname):
...     try:
...         with open('./' + fname, 'w') as f:
...             print('never get here')
...         return(0)
...     except PermissionError as e:
...         print('ERROR:
In function: '
+ inspect.stack()[0][3])
...         print('On line: {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
...         sys.exit(1)
...
>>> with mock.patch('__main__.open') as mock_oserror:
...     mock_oserror.side_effect = PermissionError
...     try:
...         open_file('testfile2.txt')
...     except SystemExit:
...         print('test passed, sys.exit() called')
...
ERROR:
In function: open_file
On line: 3 PermissionError
test passed, sys.exit() called