I am having a lot of difficulty setting my unit test. I have been using patch but it is not behaving entirely as expected.
I have a decorator at the top of my test function:
@mock.patch('WarningFactory.WarningAPIUpdate')
@mock.patch('WarningFactory.SomethingElse')
def test_send_tc_update(self, other_mock, api_mock):
However when at the end of my function when I try to make the following assertion:
api_mock.send_warning.assert_called_with('IDQ20026', 'IDQ20026')
It fails
I know that is should pass because I run
print api_mock.mock_calls
giving
[call(u'test_api'),
call().send_warning('IDQ20026', 'IDQ20026'),
call().send_warning('IDQ24500', 'IDQ24500')]
I can clearly see the send_warning method being called with the correct values, so why is my assertion failing?
Looking back now the problem was that assert_called_with only checks the most recent call.
assert_any_call(*args, **kwargs)¶ assert the mock has been called with the specified arguments.
The assert passes if the mock has ever been called, unlike assert_called_with() and assert_called_once_with() that only pass if the call is the most recent one, and in the case of assert_called_once_with() it must also be the only call.
The docs are a little dodgy as they don't mention this under the assert_called_with method.
I ended up using the assert_any_call method for my tests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With