Last updated: Apr 11, 2024
Reading timeยท4 min
Use the mock.assert_called_with()
method if you need to assert that a Mock
was called with specific arguments.
The method asserts that the last call has been made with the supplied arguments.
from unittest.mock import Mock m = Mock(return_value=None) m('bobby') m.assert_called_with('bobby') m('hadz') m.assert_called_with('hadz') m('com') m.assert_called_with('com')
The assert_called_with method asserts that the last call to the mock was made with the specified arguments.
from unittest.mock import Mock m = Mock(return_value=None) m('bobby', 'hadz', 'com') m.assert_called_with('bobby', 'hadz', 'com')
You should also specify the supplied keyword arguments when testing.
from unittest.mock import Mock m = Mock(return_value=None) m('bobby', 'hadz', 'com', foo='bar') m.assert_called_with('bobby', 'hadz', 'com', foo='bar')
The first 3 arguments in the call to the mock function are positional and the
foo
argument is a keyword argument.
We passed the arguments to the assert_called_with()
method in the exact same
way as we passed them when calling the mock.
Note that the assert_called_with
method only passes the test if the most
recent call to the mock was made with the specified arguments.
assert_called_once_with()
method insteadThere is also an assert_called_once_with method that enables you to assert that the mock was called exactly once and the call was made with the specified arguments.
from unittest.mock import Mock m = Mock(return_value=None) m('bobby', 'hadz', 'com', foo='bar') m.assert_called_once_with('bobby', 'hadz', 'com', foo='bar') m('bobby', 'hadz', 'com', foo='bar') # โ๏ธ AssertionError: Expected 'mock' to be called once. Called 2 times. m.assert_called_once_with('bobby', 'hadz', 'com', foo='bar')
The first call to the assert_called_once_with()
method succeeds because the
mock was called once and was made with the given arguments.
The second call to the method, however, fails because the mock has been called 2 times.
assert_any_call
You can also use the assert_any_call method to assert that a mock has been called with specific arguments.
from unittest.mock import Mock m = Mock(return_value=None) m('bobby', 'hadz', 'com', foo='bar') m('a', 'b', 'c') m.assert_any_call('bobby', 'hadz', 'com', foo='bar')
The assert_any_call
method passes if the mock has ever been called with the
supplied arguments.
This is not the behavior when using assert_called_with
and
assert_called_once_with
because they only pass if the most recent call was
made with the specified arguments.
The assert_any_call
method can also be used if you need to check if the mock
was called multiple times, with specific arguments in no particular order.
from unittest.mock import Mock m = Mock(return_value=None) m('bobby') m('hadz') m('com') m.assert_any_call('hadz') m.assert_any_call('bobby') m.assert_any_call('com') assert m.call_count == 3
assert_any_call()
test that the mock was called with specific
arguments in no particular order.assert
statement at the end tests that the mock was called exactly 3
times.If you need to assert successive calls to a mock method, use the
assert_has_calls()
method.
from unittest.mock import call, Mock m = Mock(return_value=None) m('bobby') m('hadz') m('com') calls = [call('bobby'), call('hadz'), call('com')] m.assert_has_calls(calls)
The assert_has_calls method asserts that the mock has been called with the specified calls.
We called the mock 3 times and asserted that it has been called 3 times with the exact same arguments.
If you reorder the calls, you'll get an error.
from unittest.mock import call, Mock m = Mock(return_value=None) m('bobby') m('hadz') m('com') # ๐๏ธ Reordered calls calls = [call('hadz'), call('bobby'), call('com')] # โ๏ธ AssertionError: Calls not found. # Expected: [call('hadz'), call('bobby'), call('com')] # Actual: [call('bobby'), call('hadz'), call('com')] m.assert_has_calls(calls)
If you don't care about the order of the calls and just want to check that the
mock has been called with the specified arguments, supply the any_order
keyword argument in the call to assert_has_calls()
.
from unittest.mock import call, Mock m = Mock(return_value=None) m('bobby') m('hadz') m('com') # ๐๏ธ Reordered calls calls = [call('hadz'), call('bobby'), call('com')] # โ Passes m.assert_has_calls(calls, any_order=True)
If the any_order
keyword argument is set to True
, the calls can be in any
order but they all must appear in the mock's calls.
If any_order
is set to False
, then the calls must be sequential.
You can view the calls of the mock by accessing the mock_calls attribute.
from unittest.mock import call, Mock m = Mock(return_value=None) m('bobby') m('hadz') m('com') # [call('bobby'), call('hadz'), call('com')] print(m.mock_calls)
The mock_calls
attribute records all calls to the mock object, its methods,
magic methods and return value mocks.
The items in the list are unittest.mock.call objects.
There is also a call_args_list attribute.
from unittest.mock import Mock m = Mock(return_value=None) m('bobby') m('hadz') m('com') m.assert_any_call('hadz') m.assert_any_call('bobby') m.assert_any_call('com') # ๐๏ธ [call('bobby'), call('hadz'), call('com')] print(m.call_args_list)
The call_args_list
attribute is a list of all of the calls made to the mock
object in sequence.
The length of the call_args_list
attribute is equal to the number of times the
mock has been called.
Just like with mock_calls
, items in the call_args_list
are
call objects.
I've also written a detailed guide on how to Mock multiple return values in a Python unit test.
You can learn more about the related topics by checking out the following tutorials:
--no-cache-dir
option