Mocking python function based on input arguments

If side_effect_func is a function then whatever that function returns is
what calls to the mock return. The side_effect_func function is called with
the same arguments as the mock. This allows you to vary the return
value of the call dynamically, based on the input:

>>> def side_effect_func(value):
...     return value + 1
...
>>> m = MagicMock(side_effect=side_effect_func)
>>> m(1)
2
>>> m(2)
3
>>> m.mock_calls
[call(1), call(2)]

http://www.voidspace.org.uk/python/mock/mock.html#calling

Leave a Comment