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

Lisa Roach webhook-mailer at python.org
Wed Feb 13 21:22:37 EST 2019


https://github.com/python/cpython/commit/2bdd5858e3f89555c8de73a0f307d63536129dbd
commit: 2bdd5858e3f89555c8de73a0f307d63536129dbd
branch: master
author: Susan Su <susansu.software at gmail.com>
committer: Lisa Roach <lisaroach14 at gmail.com>
date: 2019-02-13T18:22:29-08:00
summary:

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

files:
A Misc/NEWS.d/next/Library/2019-02-10-00-00-13.bpo-35500.1HOMmo.rst
M Lib/unittest/mock.py
M Lib/unittest/test/testmock/testmock.py

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index ef5c55d6a165..8f46050462bd 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -745,7 +745,7 @@ def _format_mock_call_signature(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)
         call_args = self.call_args
         if len(call_args) == 3:
@@ -814,7 +814,10 @@ def assert_called_with(_mock_self, *args, **kwargs):
         self = _mock_self
         if self.call_args is None:
             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():
             msg = self._format_mock_failure_message(args, kwargs)
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index 64e2fcf61c12..04ab52271578 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -724,7 +724,7 @@ def test_baseexceptional_side_effect(self):
 
     def test_assert_called_with_message(self):
         mock = Mock()
-        self.assertRaisesRegex(AssertionError, 'Not called',
+        self.assertRaisesRegex(AssertionError, 'not called',
                                 mock.assert_called_with)
 
 
@@ -917,10 +917,11 @@ def assertRaisesWithMsg(self, exception, message, func, *args, **kwargs):
     def test_assert_called_with_failure_message(self):
         mock = NonCallableMock()
 
+        actual = 'not called.'
         expected = "mock(1, '2', 3, bar='foo')"
-        message = 'Expected call: %s\nNot called'
+        message = 'expected call not found.\nExpected: %s\nActual: %s'
         self.assertRaisesWithMsg(
-            AssertionError, message % (expected,),
+            AssertionError, message % (expected, actual),
             mock.assert_called_with, 1, '2', 3, bar='foo'
         )
 
@@ -933,7 +934,7 @@ def test_assert_called_with_failure_message(self):
         for meth in asserters:
             actual = "foo(1, '2', 3, foo='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(
                 AssertionError, message % (expected, actual),
                 meth, 1, '2', 3, bar='foo'
@@ -943,7 +944,7 @@ def test_assert_called_with_failure_message(self):
         for meth in asserters:
             actual = "foo(1, '2', 3, foo='foo')"
             expected = "foo(bar='foo')"
-            message = 'Expected call: %s\nActual call: %s'
+            message = 'expected call not found.\nExpected: %s\nActual: %s'
             self.assertRaisesWithMsg(
                 AssertionError, message % (expected, actual),
                 meth, bar='foo'
@@ -953,7 +954,7 @@ def test_assert_called_with_failure_message(self):
         for meth in asserters:
             actual = "foo(1, '2', 3, foo='foo')"
             expected = "foo(1, 2, 3)"
-            message = 'Expected call: %s\nActual call: %s'
+            message = 'expected call not found.\nExpected: %s\nActual: %s'
             self.assertRaisesWithMsg(
                 AssertionError, message % (expected, actual),
                 meth, 1, 2, 3
@@ -963,7 +964,7 @@ def test_assert_called_with_failure_message(self):
         for meth in asserters:
             actual = "foo(1, '2', 3, foo='foo')"
             expected = "foo()"
-            message = 'Expected call: %s\nActual call: %s'
+            message = 'expected call not found.\nExpected: %s\nActual: %s'
             self.assertRaisesWithMsg(
                 AssertionError, message % (expected, actual), meth
             )
diff --git a/Misc/NEWS.d/next/Library/2019-02-10-00-00-13.bpo-35500.1HOMmo.rst b/Misc/NEWS.d/next/Library/2019-02-10-00-00-13.bpo-35500.1HOMmo.rst
new file mode 100644
index 000000000000..16b0fbf743b0
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-02-10-00-00-13.bpo-35500.1HOMmo.rst
@@ -0,0 +1 @@
+Write expected and actual call parameters on separate lines in :meth:`unittest.mock.Mock.assert_called_with` assertion errors.  Contributed by Susan Su.



More information about the Python-checkins mailing list