[Python-checkins] Improve error message when mock.assert_has_calls fails (GH-8205)

Gregory P. Smith webhook-mailer at python.org
Fri Aug 17 15:10:02 EDT 2018


https://github.com/python/cpython/commit/2b32da2fea1f077bb07a175f97ad8241e5605169
commit: 2b32da2fea1f077bb07a175f97ad8241e5605169
branch: master
author: davidair <davidair at users.noreply.github.com>
committer: Gregory P. Smith <greg at krypto.org>
date: 2018-08-17T12:09:58-07:00
summary:

Improve error message when mock.assert_has_calls fails (GH-8205)

This makes the assertion error message more useful, aiding debugging.

Thanks @davidair!

files:
A Misc/NEWS.d/next/Tests/2018-07-10-18-53-46.bpo-0.UBQJBc.rst
M Lib/unittest/mock.py

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 19dabddc7dfc..db1e642c00b7 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -862,7 +862,9 @@ def assert_has_calls(self, calls, any_order=False):
                 not_found.append(kall)
         if not_found:
             raise AssertionError(
-                '%r not all found in call list' % (tuple(not_found),)
+                '%r does not contain all of %r in its call list, '
+                'found %r instead' % (self._mock_name or 'mock',
+                                      tuple(not_found), all_calls)
             ) from cause
 
 
diff --git a/Misc/NEWS.d/next/Tests/2018-07-10-18-53-46.bpo-0.UBQJBc.rst b/Misc/NEWS.d/next/Tests/2018-07-10-18-53-46.bpo-0.UBQJBc.rst
new file mode 100644
index 000000000000..9d82677686bd
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2018-07-10-18-53-46.bpo-0.UBQJBc.rst
@@ -0,0 +1 @@
+Improved an error message when mock assert_has_calls fails.



More information about the Python-checkins mailing list