我希望断言一系列调用,而不关心给出的参数。有没有办法完成以下任务?
self.mocker = Mock()
self.mocker.increment = Mock()
self.mocker.decrement = Mock()
self.mocker.increment(2)
self.mocker.decrement(4)
expected_calls = [call.increment(ANY_ARGS), call.decrement(ANY_ARGS)]
self.mocker.assert_has_calls(expected_calls, any_order=False)
答案 0 :(得分:0)
您想要查看mock_calls
list并提取每次录音的名称。然后,您可以按顺序断言调用正确的方法名称:
self.assertEqual([c[0] for c in self.mocker.mock_calls], ['increment', 'decrement'])
快速演示:
>>> from unittest import mock
>>> mocker = mock.Mock()
>>> mocker.increment(2)
<Mock name='mock.increment()' id='4546399144'>
>>> mocker.decrement(4)
<Mock name='mock.decrement()' id='4546398752'>
>>> mocker.mock_calls
[call.increment(2), call.decrement(4)]
>>> mocker.mock_calls[0][0]
'increment'
>>> [c[0] for c in mocker.mock_calls]
['increment', 'decrement']