Skip to content

Commit 2bdd585

Browse files
suhearsawholisroach
authored andcommitted
bpo-35500: align expected and actual calls on mock.assert_called_with error message. (GH-11804)
1 parent 1dc5cb9 commit 2bdd585

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

Lib/unittest/mock.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ def _format_mock_call_signature(self, args, kwargs):
745745

746746

747747
def _format_mock_failure_message(self, args, kwargs):
748-
message = 'Expected call: %s\nActual call: %s'
748+
message = 'expected call not found.\nExpected: %s\nActual: %s'
749749
expected_string = self._format_mock_call_signature(args, kwargs)
750750
call_args = self.call_args
751751
if len(call_args) == 3:
@@ -814,7 +814,10 @@ def assert_called_with(_mock_self, *args, **kwargs):
814814
self = _mock_self
815815
if self.call_args is None:
816816
expected = self._format_mock_call_signature(args, kwargs)
817-
raise AssertionError('Expected call: %s\nNot called' % (expected,))
817+
actual = 'not called.'
818+
error_message = ('expected call not found.\nExpected: %s\nActual: %s'
819+
% (expected, actual))
820+
raise AssertionError(error_message)
818821

819822
def _error_message():
820823
msg = self._format_mock_failure_message(args, kwargs)

Lib/unittest/test/testmock/testmock.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ def test_baseexceptional_side_effect(self):
724724

725725
def test_assert_called_with_message(self):
726726
mock = Mock()
727-
self.assertRaisesRegex(AssertionError, 'Not called',
727+
self.assertRaisesRegex(AssertionError, 'not called',
728728
mock.assert_called_with)
729729

730730

@@ -917,10 +917,11 @@ def assertRaisesWithMsg(self, exception, message, func, *args, **kwargs):
917917
def test_assert_called_with_failure_message(self):
918918
mock = NonCallableMock()
919919

920+
actual = 'not called.'
920921
expected = "mock(1, '2', 3, bar='foo')"
921-
message = 'Expected call: %s\nNot called'
922+
message = 'expected call not found.\nExpected: %s\nActual: %s'
922923
self.assertRaisesWithMsg(
923-
AssertionError, message % (expected,),
924+
AssertionError, message % (expected, actual),
924925
mock.assert_called_with, 1, '2', 3, bar='foo'
925926
)
926927

@@ -933,7 +934,7 @@ def test_assert_called_with_failure_message(self):
933934
for meth in asserters:
934935
actual = "foo(1, '2', 3, foo='foo')"
935936
expected = "foo(1, '2', 3, bar='foo')"
936-
message = 'Expected call: %s\nActual call: %s'
937+
message = 'expected call not found.\nExpected: %s\nActual: %s'
937938
self.assertRaisesWithMsg(
938939
AssertionError, message % (expected, actual),
939940
meth, 1, '2', 3, bar='foo'
@@ -943,7 +944,7 @@ def test_assert_called_with_failure_message(self):
943944
for meth in asserters:
944945
actual = "foo(1, '2', 3, foo='foo')"
945946
expected = "foo(bar='foo')"
946-
message = 'Expected call: %s\nActual call: %s'
947+
message = 'expected call not found.\nExpected: %s\nActual: %s'
947948
self.assertRaisesWithMsg(
948949
AssertionError, message % (expected, actual),
949950
meth, bar='foo'
@@ -953,7 +954,7 @@ def test_assert_called_with_failure_message(self):
953954
for meth in asserters:
954955
actual = "foo(1, '2', 3, foo='foo')"
955956
expected = "foo(1, 2, 3)"
956-
message = 'Expected call: %s\nActual call: %s'
957+
message = 'expected call not found.\nExpected: %s\nActual: %s'
957958
self.assertRaisesWithMsg(
958959
AssertionError, message % (expected, actual),
959960
meth, 1, 2, 3
@@ -963,7 +964,7 @@ def test_assert_called_with_failure_message(self):
963964
for meth in asserters:
964965
actual = "foo(1, '2', 3, foo='foo')"
965966
expected = "foo()"
966-
message = 'Expected call: %s\nActual call: %s'
967+
message = 'expected call not found.\nExpected: %s\nActual: %s'
967968
self.assertRaisesWithMsg(
968969
AssertionError, message % (expected, actual), meth
969970
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write expected and actual call parameters on separate lines in :meth:`unittest.mock.Mock.assert_called_with` assertion errors. Contributed by Susan Su.

0 commit comments

Comments
 (0)