首先,我使用pytest-mock和pytest包来测试我正在处理的软件项目 .
我有一种情况,我在模块中测试一个类,并试图模拟一个它从 __init__
方法实例化的复合对象 . 当我这样做时,我能够断言正确调用 __init__
方法,但是当我尝试断言调用实例方法时,我得到一个断言错误 .
当我通过将 --pdb
选项传递给它来调试pytest时,我通过检查 MagicMock()
对象的 .method.called
属性来验证它是"wasn't",但是当我检查 MagicMock()
对象的 .method_calls
属性时,我看到它实际上被调用了 .
这是一个显示我的意思的简单示例:
test.py
import module
def test_Calling(mocker):
mocker.patch('module.Called', autospec=True)
module.Calling('argument')
module.Called.assert_called_once_with('argument')
module.Called.check_me.assert_called_once()
module.py
class Calling(object):
def __init__(self, argument):
called_instance = Called(argument)
called_instance.check_me()
class Called(object):
def __init__(self, argument):
pass
def check_me(self):
pass
这是pytest / pdb会话:
(venv) C:\pytest-issue>pytest --pdb test.py
============================= test session starts =============================
platform win32 -- Python 2.7.12, pytest-3.0.6, py-1.4.32, pluggy-0.4.0
rootdir: C:\pytest-issue, inifile:
plugins: mock-1.5.0
collected 1 items
test.py F
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> traceback >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
mocker = <pytest_mock.MockFixture object at 0x000000000431B2E8>
def test_Calling(mocker):
mocker.patch('module.Called', autospec=True)
module.Calling('argument')
module.Called.assert_called_once_with('argument')
> module.Called.check_me.assert_called_once()
test.py:9:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_mock_self = <MagicMock name='Called.check_me' spec='instancemethod' id='70367608'>
def assert_called_once(_mock_self):
"""assert that the mock was called only once.
"""
self = _mock_self
if not self.call_count == 1:
msg = ("Expected '%s' to have been called once. Called %s times." %
(self._mock_name or 'mock', self.call_count))
> raise AssertionError(msg)
E AssertionError: Expected 'check_me' to have been called once. Called 0 times.
venv\lib\site-packages\mock\mock.py:915: AssertionError
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> c:\pytest-issue\venv\lib\site-packages\mock\mock.py(915)assert_called_once()
-> raise AssertionError(msg)
(Pdb) up
> c:\pytest-issue\test.py(9)test_Calling()
-> module.Called.check_me.assert_called_once()
(Pdb) module.Called.method_calls
[call().check_me()]
(Pdb) module.Called.check_me.called
False
对于我的生活,我做错了 . 在这种情况下,我是否需要断言 .Called.method_calls
?如果是这样,看起来我必须在我的测试中加入大量逻辑,这是不可取的 .
1 回答
所以,事实证明我对你如何测试修补模块一无所知 .
这里要考虑的重要事项是你需要存储mocker.patch的返回值,因为这将提供对
MagicMock
对象的引用,该对象在module.Called
上被实例化和修补 . 接下来,当您希望测试正在调用实例方法时,您应该在MagicMock
实例上使用assert_has_calls
方法,并传递反映所调用内容的call()
参数 .call()
本身引用__new__/__init__
调用,而call().method
引用针对实例方法的调用 .由于搞清楚了,我已经能够非常成功地使用
pytest-mock
模块了 .