]> granicus.if.org Git - python/commitdiff
bpo-36871: Avoid duplicated 'Actual:' in assertion message (GH-16361)
authorSamuel Freilich <sfreilich@google.com>
Tue, 24 Sep 2019 22:04:29 +0000 (18:04 -0400)
committerGregory P. Smith <greg@krypto.org>
Tue, 24 Sep 2019 22:04:29 +0000 (15:04 -0700)
Fixes an issue caught after merge of PR 16005.

Tightened test assertions to check the entire assertion message.

Lib/unittest/mock.py
Lib/unittest/test/testmock/testasync.py
Lib/unittest/test/testmock/testmock.py

index 7bd11c8e703412ec3a0b4fad56a8f6a71d052971..0cd7af65b94020e0b045c02bbc15d584c5706454 100644 (file)
@@ -939,8 +939,8 @@ class NonCallableMock(Base):
                                     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
 
index f951526dbeb8a09785450ed3ab1b474f6d6eeab5..fde1e4a690b2709101cb863ef21f7c29ac295a67 100644 (file)
@@ -892,21 +892,28 @@ class AsyncMockAssert(unittest.TestCase):
             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)
index 88807d71ccb06ceb3e8264212868526504ceb407..01bc4794446521cb7b0e6da37947a4b0ed84c6ba 100644 (file)
@@ -1436,23 +1436,30 @@ class MockTest(unittest.TestCase):
         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):