您可以添加
assert_not_called_with
方法到
unittest.mock.Mock
from unittest.mock import Mock
def assert_not_called_with(self, *args, **kwargs):
try:
self.assert_called_with(*args, **kwargs)
except AssertionError:
return
raise AssertionError('Expected %s to not have been called.' % self._format_mock_call_signature(args, kwargs))
Mock.assert_not_called_with = assert_not_called_with
以便:
m = Mock()
m.assert_not_called_with(1, 2, a=3)
m(3, 4, b=5)
m.assert_not_called_with(3, 4, b=5)
输出:
AssertionError: Expected mock(3, 4, b=5) to not have been called.