unittest.mock.Mock.called

called A boolean representing whether or not the mock object has been called: >>> mock = Mock(return_value=None) >>> mock.called False >>> mock() >>> mock.called True

unittest.mock.Mock.call_args_list

call_args_list This is a list of all the calls made to the mock object in sequence (so the length of the list is the number of times it has been called). Before any calls have been made it is an empty list. The call object can be used for conveniently constructing lists of calls to compare with call_args_list. >>> mock = Mock(return_value=None) >>> mock() >>> mock(3, 4) >>> mock(key='fish', next='w00t!') >>> mock.call_args_list [call(), call(3, 4)

unittest.mock.Mock.call_count

call_count An integer telling you how many times the mock object has been called: >>> mock = Mock(return_value=None) >>> mock.call_count 0 >>> mock() >>> mock() >>> mock.call_count 2

unittest.mock.Mock.call_args

call_args This is either None (if the mock hasn’t been called), or the arguments that the mock was last called with. This will be in the form of a tuple: the first member is any ordered arguments the mock was called with (or an empty tuple) and the second member is any keyword arguments (or an empty dictionary). >>> mock = Mock(return_value=None) >>> print(mock.call_args) None >>> mock() >>> mock.call_args call() >>> mock.call_args == () True >

unittest.mock.Mock.assert_called_with()

assert_called_with(*args, **kwargs) This method is a convenient way of asserting that calls are made in a particular way: >>> mock = Mock() >>> mock.method(1, 2, 3, test='wow') <Mock name='mock.method()' id='...'> >>> mock.method.assert_called_with(1, 2, 3, test='wow')

unittest.mock.Mock.assert_has_calls()

assert_has_calls(calls, any_order=False) assert the mock has been called with the specified calls. The mock_calls list is checked for the calls. If any_order is false (the default) then the calls must be sequential. There can be extra calls before or after the specified calls. If any_order is true then the calls can be in any order, but they must all appear in mock_calls. >>> mock = Mock(return_value=None) >>> mock(1) >>> mock(2) >>> mock(3) >>> m

unittest.mock.Mock.attach_mock()

attach_mock(mock, attribute) Attach a mock as an attribute of this one, replacing its name and parent. Calls to the attached mock will be recorded in the method_calls and mock_calls attributes of this one.

unittest.mock.Mock.assert_called_once_with()

assert_called_once_with(*args, **kwargs) Assert that the mock was called exactly once and with the specified arguments. >>> mock = Mock(return_value=None) >>> mock('foo', bar='baz') >>> mock.assert_called_once_with('foo', bar='baz') >>> mock('foo', bar='baz') >>> mock.assert_called_once_with('foo', bar='baz') Traceback (most recent call last): ... AssertionError: Expected 'mock' to be called once. Called 2 times.

unittest.mock.Mock.assert_not_called()

assert_not_called() Assert the mock was never called. >>> m = Mock() >>> m.hello.assert_not_called() >>> obj = m.hello() >>> m.hello.assert_not_called() Traceback (most recent call last): ... AssertionError: Expected 'hello' to not have been called. Called 1 times. New in version 3.5.

unittest.mock.Mock.assert_any_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. >>> mock = Mock(return_value=None) >>> mock(1, 2, arg='thing') >>> mock('some', 'thing', 'else') >>> mock.assert_any_call(1, 2, arg='thing')