python mock assert called n times

Solutions on MaxInterview for python mock assert called n times by the best coders in the world

showing results for - "python mock assert called n times"
Samuel
29 Oct 2018
1assert_has_calls is another approach to this problem.
2
3From the docs:
4
5assert_has_calls (calls, any_order=False)
6
7assert the mock has been called with the specified calls. The mock_calls list is checked for the calls.
8
9If any_order is False (the default) then the calls must be sequential. There can be extra calls before or after the specified calls.
10
11If any_order is True then the calls can be in any order, but they must all appear in mock_calls.
12
13Example:
14
15>>> from unittest.mock import call, Mock
16>>> mock = Mock(return_value=None)
17>>> mock(1)
18>>> mock(2)
19>>> mock(3)
20>>> mock(4)
21>>> calls = [call(2), call(3)]
22>>> mock.assert_has_calls(calls)
23>>> calls = [call(4), call(2), call(3)]
24>>> mock.assert_has_calls(calls, any_order=True)
25Source: https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.assert_has_calls
similar questions
queries leading to this page
python mock assert called n times