def test_any_mock_calls_comparison_order(self): mock = Mock() class Foo(object): def __eq__(self, other): pass def __ne__(self, other): pass for d in datetime.now(), Foo(): mock.reset_mock() mock(d, foo=d, bar=d) mock.method(d, zinga=d, alpha=d) mock().method(a1=d, z99=d) expected = [ call(ANY, foo=ANY, bar=ANY), call.method(ANY, zinga=ANY, alpha=ANY), call(), call().method(a1=ANY, z99=ANY) ] self.assertEqual(expected, mock.mock_calls) self.assertEqual(mock.mock_calls, expected)
def test_method_calls(self): mock = Mock() mock.method() mock.property.method.attribute() self.assertListEqual( [call.method(), call.property.method.attribute()], mock.method_calls)
def test_any_mock_calls_comparison_order(self): mock = Mock() d = datetime.now() class Foo(object): __qualname__ = 'AnyTest.test_any_mock_calls_comparison_order.<locals>.Foo' def __eq__(self, other): return False def __ne__(self, other): return True for d in (datetime.now(), Foo()): mock.reset_mock() mock(d, foo=d, bar=d) mock.method(d, zinga=d, alpha=d) mock().method(a1=d, z99=d) expected = [call(ANY, foo=ANY, bar=ANY), call.method(ANY, zinga=ANY, alpha=ANY), call(), call().method(a1=ANY, z99=ANY)] self.assertEqual(expected, mock.mock_calls) self.assertEqual(mock.mock_calls, expected)
### testing the order and the method calls from unittest.mock import MagicMock, call mock = MagicMock() mock.john() mock.method() # mock.john() mock.attribute.method(10, x=53) print(mock.mock_calls) mock.assert_has_calls( [call.method(), call.john(), call.attribute.method(10, x=53)], False)