https://github.com/python/cpython/commit/1600d78e2d090319930c6538b496ffcca120a696
commit: 1600d78e2d090319930c6538b496ffcca120a696
branch: main
author: wookie184 <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-01-04T21:11:34+02:00
summary:

gh-113569: Display calls in Mock.assert_has_calls failure when empty (GH-113573)

files:
A Misc/NEWS.d/next/Library/2023-12-29-17-57-45.gh-issue-113569.qcRCEI.rst
M Lib/test/test_unittest/testmock/testmock.py
M Lib/unittest/mock.py

diff --git a/Lib/test/test_unittest/testmock/testmock.py 
b/Lib/test/test_unittest/testmock/testmock.py
index 6af8acc3b0617e..1725406bcfb9e4 100644
--- a/Lib/test/test_unittest/testmock/testmock.py
+++ b/Lib/test/test_unittest/testmock/testmock.py
@@ -1547,25 +1547,33 @@ def f(x=None): pass
         mock = Mock(spec=f)
         mock(1)
 
-        with self.assertRaisesRegex(
-                AssertionError,
-                '^{}$'.format(
-                    re.escape('Calls not found.\n'
-                              'Expected: [call()]\n'
-                              '  Actual: [call(1)]'))) as cm:
+        with self.assertRaises(AssertionError) as cm:
             mock.assert_has_calls([call()])
+        self.assertEqual(str(cm.exception),
+            'Calls not found.\n'
+            'Expected: [call()]\n'
+            '  Actual: [call(1)]'
+        )
         self.assertIsNone(cm.exception.__cause__)
 
+        uncalled_mock = Mock()
+        with self.assertRaises(AssertionError) as cm:
+            uncalled_mock.assert_has_calls([call()])
+        self.assertEqual(str(cm.exception),
+            'Calls not found.\n'
+            'Expected: [call()]\n'
+            '  Actual: []'
+        )
+        self.assertIsNone(cm.exception.__cause__)
 
-        with self.assertRaisesRegex(
-                AssertionError,
-                '^{}$'.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:
+        with self.assertRaises(AssertionError) as cm:
             mock.assert_has_calls([call(), call(1, 2)])
+        self.assertEqual(str(cm.exception),
+            'Error processing expected calls.\n'
+            "Errors: [None, TypeError('too many positional arguments')]\n"
+            'Expected: [call(), call(1, 2)]\n'
+            '  Actual: [call(1)]'
+        )
         self.assertIsInstance(cm.exception.__cause__, TypeError)
 
     def test_assert_any_call(self):
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 2adb3d70662b1a..93f4d9743ed2fa 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1010,8 +1010,8 @@ def assert_has_calls(self, calls, any_order=False):
                                     for e in expected])
                 raise AssertionError(
                     f'{problem}\n'
-                    f'Expected: {_CallList(calls)}'
-                    f'{self._calls_repr(prefix="  Actual").rstrip(".")}'
+                    f'Expected: {_CallList(calls)}\n'
+                    f'  Actual: {safe_repr(self.mock_calls)}'
                 ) from cause
             return
 
@@ -1085,7 +1085,7 @@ def _get_child_mock(self, /, **kw):
         return klass(**kw)
 
 
-    def _calls_repr(self, prefix="Calls"):
+    def _calls_repr(self):
         """Renders self.mock_calls as a string.
 
         Example: "\nCalls: [call(1), call(2)]."
@@ -1095,7 +1095,7 @@ def _calls_repr(self, prefix="Calls"):
         """
         if not self.mock_calls:
             return ""
-        return f"\n{prefix}: {safe_repr(self.mock_calls)}."
+        return f"\nCalls: {safe_repr(self.mock_calls)}."
 
 
 # Denylist for forbidden attribute names in safe mode
diff --git 
a/Misc/NEWS.d/next/Library/2023-12-29-17-57-45.gh-issue-113569.qcRCEI.rst 
b/Misc/NEWS.d/next/Library/2023-12-29-17-57-45.gh-issue-113569.qcRCEI.rst
new file mode 100644
index 00000000000000..9b63fc940991fe
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-12-29-17-57-45.gh-issue-113569.qcRCEI.rst
@@ -0,0 +1,2 @@
+Indicate if there were no actual calls in unittest
+:meth:`~unittest.mock.Mock.assert_has_calls` failure.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to