【发布时间】:2017-10-24 15:10:40
【问题描述】:
我能够成功地模拟一个函数,并且我确信原始函数没有被调用。我在原始函数中添加了一个巨大的 print 语句,当我模拟它时,这个 print 没有被调用。当我重新打开模拟时,不会调用 print 语句。
但是,我的 assert_call 没有说它从未被调用过。有没有人经历过这样的事情?
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
我的错误回复
AssertionError: Expected 'helper_function' to have been called.
更新 我刚刚在断言之前添加了以下几行
print mock_cw.method_calls
print mock_cw.mock_calls
method_calls 是一个空列表,而 mock_calls 是一个包含 1 项的列表
[call(arg1_expected_for_helper_fn, arg2_expected_for_helper_fn)]
但断言仍然失败
【问题讨论】:
-
My_Obj.do_something() 是否调用了 My_Obj.helper_function()?你能把那个代码的sn-p也放上来吗?
-
是的,确实如此。我在评论中提到了这一点。该代码非常简单,我不知道这是否会有所帮助。看看我要用什么来更新问题。这可能会有所帮助。
标签: python python-2.7 unit-testing mocking