Python 属性为 "called" 的模拟方法在调用方法后仍然为 False
Python mock method attributed "called" is still False after method is called
我正在测试使用 Python 模拟库调用的方法。外面的方法是这样的:
def get_abc():
get_a()
get_b()
get_c(False)
测试用例是这样的:
@mock.patch('myclass.get_a')
@mock.patch('myclass.get_b')
@mock.patch('myclass.get_c')
def test_inner_methods(self, mock_meth_1, mock_meth_2, mock_meth_3):
o = Outerclass(config_file=cfg)
o._get_abc()
self.assertTrue(mock_meth_1.called)
mock_meth_1.assert_called_with(False)
当我在调试时,get_c() 被成功调用,但 mock_meth_1 的被调用属性从未改变。我需要做更多的事情来正确模拟该方法吗?
你修补了 myclass.get_c
两次,所以我不知道它会如何表现,但这可能不是你想要做的。将其中之一切换为 myclass.get_a
,您可能会没事的。
您可能还会发现 mock_meth1.assert_called()
比 self.assertTrue(mock_meth_1.called)
更容易。
我正在测试使用 Python 模拟库调用的方法。外面的方法是这样的:
def get_abc():
get_a()
get_b()
get_c(False)
测试用例是这样的:
@mock.patch('myclass.get_a')
@mock.patch('myclass.get_b')
@mock.patch('myclass.get_c')
def test_inner_methods(self, mock_meth_1, mock_meth_2, mock_meth_3):
o = Outerclass(config_file=cfg)
o._get_abc()
self.assertTrue(mock_meth_1.called)
mock_meth_1.assert_called_with(False)
当我在调试时,get_c() 被成功调用,但 mock_meth_1 的被调用属性从未改变。我需要做更多的事情来正确模拟该方法吗?
你修补了 myclass.get_c
两次,所以我不知道它会如何表现,但这可能不是你想要做的。将其中之一切换为 myclass.get_a
,您可能会没事的。
您可能还会发现 mock_meth1.assert_called()
比 self.assertTrue(mock_meth_1.called)
更容易。