Commit 2bdd5858 authored by Susan Su's avatar Susan Su Committed by Lisa Roach

bpo-35500: align expected and actual calls on mock.assert_called_with error message. (GH-11804)

parent 1dc5cb9c
...@@ -745,7 +745,7 @@ class NonCallableMock(Base): ...@@ -745,7 +745,7 @@ class NonCallableMock(Base):
def _format_mock_failure_message(self, args, kwargs): def _format_mock_failure_message(self, args, kwargs):
message = 'Expected call: %s\nActual call: %s' message = 'expected call not found.\nExpected: %s\nActual: %s'
expected_string = self._format_mock_call_signature(args, kwargs) expected_string = self._format_mock_call_signature(args, kwargs)
call_args = self.call_args call_args = self.call_args
if len(call_args) == 3: if len(call_args) == 3:
...@@ -814,7 +814,10 @@ class NonCallableMock(Base): ...@@ -814,7 +814,10 @@ class NonCallableMock(Base):
self = _mock_self self = _mock_self
if self.call_args is None: if self.call_args is None:
expected = self._format_mock_call_signature(args, kwargs) expected = self._format_mock_call_signature(args, kwargs)
raise AssertionError('Expected call: %s\nNot called' % (expected,)) actual = 'not called.'
error_message = ('expected call not found.\nExpected: %s\nActual: %s'
% (expected, actual))
raise AssertionError(error_message)
def _error_message(): def _error_message():
msg = self._format_mock_failure_message(args, kwargs) msg = self._format_mock_failure_message(args, kwargs)
......
...@@ -724,7 +724,7 @@ class MockTest(unittest.TestCase): ...@@ -724,7 +724,7 @@ class MockTest(unittest.TestCase):
def test_assert_called_with_message(self): def test_assert_called_with_message(self):
mock = Mock() mock = Mock()
self.assertRaisesRegex(AssertionError, 'Not called', self.assertRaisesRegex(AssertionError, 'not called',
mock.assert_called_with) mock.assert_called_with)
...@@ -917,10 +917,11 @@ class MockTest(unittest.TestCase): ...@@ -917,10 +917,11 @@ class MockTest(unittest.TestCase):
def test_assert_called_with_failure_message(self): def test_assert_called_with_failure_message(self):
mock = NonCallableMock() mock = NonCallableMock()
actual = 'not called.'
expected = "mock(1, '2', 3, bar='foo')" expected = "mock(1, '2', 3, bar='foo')"
message = 'Expected call: %s\nNot called' message = 'expected call not found.\nExpected: %s\nActual: %s'
self.assertRaisesWithMsg( self.assertRaisesWithMsg(
AssertionError, message % (expected,), AssertionError, message % (expected, actual),
mock.assert_called_with, 1, '2', 3, bar='foo' mock.assert_called_with, 1, '2', 3, bar='foo'
) )
...@@ -933,7 +934,7 @@ class MockTest(unittest.TestCase): ...@@ -933,7 +934,7 @@ class MockTest(unittest.TestCase):
for meth in asserters: for meth in asserters:
actual = "foo(1, '2', 3, foo='foo')" actual = "foo(1, '2', 3, foo='foo')"
expected = "foo(1, '2', 3, bar='foo')" expected = "foo(1, '2', 3, bar='foo')"
message = 'Expected call: %s\nActual call: %s' message = 'expected call not found.\nExpected: %s\nActual: %s'
self.assertRaisesWithMsg( self.assertRaisesWithMsg(
AssertionError, message % (expected, actual), AssertionError, message % (expected, actual),
meth, 1, '2', 3, bar='foo' meth, 1, '2', 3, bar='foo'
...@@ -943,7 +944,7 @@ class MockTest(unittest.TestCase): ...@@ -943,7 +944,7 @@ class MockTest(unittest.TestCase):
for meth in asserters: for meth in asserters:
actual = "foo(1, '2', 3, foo='foo')" actual = "foo(1, '2', 3, foo='foo')"
expected = "foo(bar='foo')" expected = "foo(bar='foo')"
message = 'Expected call: %s\nActual call: %s' message = 'expected call not found.\nExpected: %s\nActual: %s'
self.assertRaisesWithMsg( self.assertRaisesWithMsg(
AssertionError, message % (expected, actual), AssertionError, message % (expected, actual),
meth, bar='foo' meth, bar='foo'
...@@ -953,7 +954,7 @@ class MockTest(unittest.TestCase): ...@@ -953,7 +954,7 @@ class MockTest(unittest.TestCase):
for meth in asserters: for meth in asserters:
actual = "foo(1, '2', 3, foo='foo')" actual = "foo(1, '2', 3, foo='foo')"
expected = "foo(1, 2, 3)" expected = "foo(1, 2, 3)"
message = 'Expected call: %s\nActual call: %s' message = 'expected call not found.\nExpected: %s\nActual: %s'
self.assertRaisesWithMsg( self.assertRaisesWithMsg(
AssertionError, message % (expected, actual), AssertionError, message % (expected, actual),
meth, 1, 2, 3 meth, 1, 2, 3
...@@ -963,7 +964,7 @@ class MockTest(unittest.TestCase): ...@@ -963,7 +964,7 @@ class MockTest(unittest.TestCase):
for meth in asserters: for meth in asserters:
actual = "foo(1, '2', 3, foo='foo')" actual = "foo(1, '2', 3, foo='foo')"
expected = "foo()" expected = "foo()"
message = 'Expected call: %s\nActual call: %s' message = 'expected call not found.\nExpected: %s\nActual: %s'
self.assertRaisesWithMsg( self.assertRaisesWithMsg(
AssertionError, message % (expected, actual), meth AssertionError, message % (expected, actual), meth
) )
......
Write expected and actual call parameters on separate lines in :meth:`unittest.mock.Mock.assert_called_with` assertion errors. Contributed by Susan Su.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment