Tyl*_*r R 5 python unit-testing mocking python-2.7
我能够成功模拟一个函数,并且我确信原始函数没有被调用。我在原始函数中添加了一个巨大的 print 语句,当我模拟它时,不会调用此 print 。当我重新打开模拟时,不会调用打印语句。
但是,我的断言调用失败,说它从未被调用过。有人经历过这样的事情吗?
class FooTestCase(unittest.TestCase):
@mock.patch('MyObj.helper_function')
def test_simple(self, mock_hf):
my_obj = MyObj()
# internally, this class imports HelperModule
# and the method calls helper_function
my_obj.do_something()
mock_hf.helper_function.assert_called()
return
Run Code Online (Sandbox Code Playgroud)
我的错误响应
AssertionError: Expected 'helper_function' to have been called.
Run Code Online (Sandbox Code Playgroud)
更新 我刚刚在断言之前添加了以下几行
print mock_cw.method_calls
print mock_cw.mock_calls
Run Code Online (Sandbox Code Playgroud)
method_calls 是一个空列表,而mock_calls 是一个包含 1 项的列表,即
[call(arg1_expected_for_helper_fn, arg2_expected_for_helper_fn)]
Run Code Online (Sandbox Code Playgroud)
但断言仍然失败
问题是我正在检查是否mock_hf.helper_function被调用,但mock_hf已经映射到helper_function. 我或多或少地检查了被helper_function.helper_function调用的内容而不仅仅是helper_function.
断言行需要是
mock_hf.assert_called()
| 归档时间: |
|
| 查看次数: |
8153 次 |
| 最近记录: |