[Python-checkins] bpo-36871: Avoid duplicated 'Actual:' in assertion message (GH-16361)

Gregory P. Smith webhook-mailer at python.org
Tue Sep 24 18:04:33 EDT 2019


https://github.com/python/cpython/commit/2180f6b058effbf49ec819f7cedbe76ddd4b700c
commit: 2180f6b058effbf49ec819f7cedbe76ddd4b700c
branch: master
author: Samuel Freilich <sfreilich at google.com>
committer: Gregory P. Smith <greg at krypto.org>
date: 2019-09-24T15:04:29-07:00
summary:

bpo-36871: Avoid duplicated 'Actual:' in assertion message (GH-16361)

Fixes an issue caught after merge of PR 16005.

Tightened test assertions to check the entire assertion message.

files:
M Lib/unittest/mock.py
M Lib/unittest/test/testmock/testasync.py
M Lib/unittest/test/testmock/testmock.py

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 7bd11c8e7034..0cd7af65b940 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -939,8 +939,8 @@ def assert_has_calls(self, calls, any_order=False):
                                     for e in expected])
                 raise AssertionError(
                     f'{problem}\n'
-                    f'Expected: {_CallList(calls)}\n'
-                    f'Actual: {self._calls_repr(prefix="Actual")}'
+                    f'Expected: {_CallList(calls)}'
+                    f'{self._calls_repr(prefix="Actual").rstrip(".")}'
                 ) from cause
             return
 
diff --git a/Lib/unittest/test/testmock/testasync.py b/Lib/unittest/test/testmock/testasync.py
index f951526dbeb8..fde1e4a690b2 100644
--- a/Lib/unittest/test/testmock/testasync.py
+++ b/Lib/unittest/test/testmock/testasync.py
@@ -892,21 +892,28 @@ def test_assert_not_awaited(self):
             self.mock.assert_not_awaited()
 
     def test_assert_has_awaits_not_matching_spec_error(self):
-        async def f(): pass
+        async def f(x=None): pass
 
-        mock = AsyncMock(spec=f)
+        self.mock = AsyncMock(spec=f)
+        asyncio.run(self._runnable_test(1))
 
         with self.assertRaisesRegex(
                 AssertionError,
-                re.escape('Awaits not found.\nExpected:')) as cm:
-            mock.assert_has_awaits([call()])
+                '^{}$'.format(
+                    re.escape('Awaits not found.\n'
+                              'Expected: [call()]\n'
+                              'Actual: [call(1)]'))) as cm:
+            self.mock.assert_has_awaits([call()])
         self.assertIsNone(cm.exception.__cause__)
 
         with self.assertRaisesRegex(
                 AssertionError,
-                re.escape('Error processing expected awaits.\n'
-                          "Errors: [None, TypeError('too many positional "
-                          "arguments')]\n"
-                          'Expected:')) as cm:
-            mock.assert_has_awaits([call(), call('wrong')])
+                '^{}$'.format(
+                    re.escape(
+                        'Error processing expected awaits.\n'
+                        "Errors: [None, TypeError('too many positional "
+                        "arguments')]\n"
+                        'Expected: [call(), call(1, 2)]\n'
+                        'Actual: [call(1)]'))) as cm:
+            self.mock.assert_has_awaits([call(), call(1, 2)])
         self.assertIsInstance(cm.exception.__cause__, TypeError)
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index 88807d71ccb0..01bc47944465 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -1436,23 +1436,30 @@ def f(a, b, c, d=None): pass
         mock.assert_has_calls(calls[:-1], any_order=True)
 
     def test_assert_has_calls_not_matching_spec_error(self):
-        def f(): pass
+        def f(x=None): pass
 
         mock = Mock(spec=f)
+        mock(1)
 
         with self.assertRaisesRegex(
                 AssertionError,
-                re.escape('Calls not found.\nExpected:')) as cm:
+                '^{}$'.format(
+                    re.escape('Calls not found.\n'
+                              'Expected: [call()]\n'
+                              'Actual: [call(1)]'))) as cm:
             mock.assert_has_calls([call()])
         self.assertIsNone(cm.exception.__cause__)
 
+
         with self.assertRaisesRegex(
                 AssertionError,
-                re.escape('Error processing expected calls.\n'
-                          "Errors: [None, TypeError('too many positional "
-                          "arguments')]\n"
-                          'Expected:')) as cm:
-            mock.assert_has_calls([call(), call('wrong')])
+                '^{}$'.format(
+                    re.escape(
+                        'Error processing expected calls.\n'
+                        "Errors: [None, TypeError('too many positional arguments')]\n"
+                        "Expected: [call(), call(1, 2)]\n"
+                        'Actual: [call(1)]'))) as cm:
+            mock.assert_has_calls([call(), call(1, 2)])
         self.assertIsInstance(cm.exception.__cause__, TypeError)
 
     def test_assert_any_call(self):



More information about the Python-checkins mailing list