【发布时间】:2018-06-06 12:59:29
【问题描述】:
我要对函数进行单元测试
def function(self, timeout):
self.method1(self.method2(self.PARAM), timeout=timeout)
我目前的单元测试是
patcher = patch("x.x.x.method2")
method2_mock = patcher.start()
self.addCleanup(patcher.stop)
...
@patch("x.x.x.method1")
def test_function(self, method1_mock):
timeout = 1
self.method2_mock.return_value = "val"
self.page.function(timeout)
self.method2_mock.assert_called_once_with(self.page.PARAM)
method1_mock.assert_called_once_with("val", timeout=1)
在运行测试时,我在实际测试的函数中的method1出现以下错误:
Traceback (most recent call last):
File "~/.local/lib/python2.7/site-packages/mock/mock.py", line 1305, in patched
return func(*args, **keywargs)
...
File "~/.local/lib/python2.7/site-packages/mock/mock.py", line 1062, in __call__
return _mock_self._mock_call(*args, **kwargs)
File "~/.local/lib/python2.7/site-packages/mock/mock.py", line 1128, in _mock_call
ret_val = effect(*args, **kwargs)
TypeError: local_side_effect() got an unexpected keyword argument 'timeout'
为什么我的模拟方法 1 不接受关键字参数,我该如何解决这个问题?
【问题讨论】:
标签: python python-2.7 mocking python-unittest keyword-argument