Judging from other answers, no one except @rob-kennedy talked about the call_args_list
.
It's a powerful tool for that you can implement the exact contrary of MagicMock.assert_called_with()
call_args_list
is a list of call
objects. Each call
object represents a call made on a mocked callable.
>>> from unittest.mock import MagicMock
>>> m = MagicMock()
>>> m.call_args_list
[]
>>> m(42)
<MagicMock name='mock()' id='139675158423872'>
>>> m.call_args_list
[call(42)]
>>> m(42, 30)
<MagicMock name='mock()' id='139675158423872'>
>>> m.call_args_list
[call(42), call(42, 30)]
Consuming a call
object is easy, since you can compare it with a tuple of length 2 where the first component is a tuple containing all the positional arguments of the related call, while the second component is a dictionary of the keyword arguments.
>>> ((42,),) in m.call_args_list
True
>>> m(42, foo='bar')
<MagicMock name='mock()' id='139675158423872'>
>>> ((42,), {'foo': 'bar'}) in m.call_args_list
True
>>> m(foo='bar')
<MagicMock name='mock()' id='139675158423872'>
>>> ((), {'foo': 'bar'}) in m.call_args_list
True
So, a way to address the specific problem of the OP is
def test_something():
with patch('something') as my_var:
assert ((some, args),) not in my_var.call_args_list
Note that this way, instead of just checking if a mocked callable has been called, via MagicMock.called
, you can now check if it has been called with a specific set of arguments.
That's useful. Say you want to test a function that takes a list and call another function, compute()
, for each of the value of the list only if they satisfy a specific condition.
You can now mock compute
, and test if it has been called on some value but not on others.