[Python-checkins] cpython: Closes issue 15323. Improve failure message of Mock.assert_called_once_with

michael.foord python-checkins at python.org
Fri Sep 28 17:16:37 CEST 2012


http://hg.python.org/cpython/rev/70d43fedb2d7
changeset:   79219:70d43fedb2d7
parent:      79217:3bb53816f9c5
user:        Michael Foord <michael at voidspace.org.uk>
date:        Fri Sep 28 16:15:22 2012 +0100
summary:
  Closes issue 15323. Improve failure message of Mock.assert_called_once_with

files:
  Doc/library/unittest.mock.rst          |  4 ++--
  Lib/unittest/mock.py                   |  4 ++--
  Lib/unittest/test/testmock/testmock.py |  7 +++++++
  Misc/NEWS                              |  2 ++
  4 files changed, 13 insertions(+), 4 deletions(-)


diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst
--- a/Doc/library/unittest.mock.rst
+++ b/Doc/library/unittest.mock.rst
@@ -276,7 +276,7 @@
             >>> mock.assert_called_once_with('foo', bar='baz')
             Traceback (most recent call last):
               ...
-            AssertionError: Expected to be called once. Called 2 times.
+            AssertionError: Expected 'mock' to be called once. Called 2 times.
 
 
     .. method:: assert_any_call(*args, **kwargs)
@@ -2020,7 +2020,7 @@
     >>> mock.assert_called_once_with(1, 2, 3)
     Traceback (most recent call last):
      ...
-    AssertionError: Expected to be called once. Called 2 times.
+    AssertionError: Expected 'mock' to be called once. Called 2 times.
 
 Because mocks auto-create attributes on demand, and allow you to call them
 with arbitrary arguments, if you misspell one of these assert methods then
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -731,8 +731,8 @@
         arguments."""
         self = _mock_self
         if not self.call_count == 1:
-            msg = ("Expected to be called once. Called %s times." %
-                   self.call_count)
+            msg = ("Expected '%s' to be called once. Called %s times." %
+                   (self._mock_name or 'mock', self.call_count))
             raise AssertionError(msg)
         return self.assert_called_with(*args, **kwargs)
 
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -463,6 +463,13 @@
                                 mock.assert_called_with)
 
 
+    def test_assert_called_once_with_message(self):
+        mock = Mock(name='geoffrey')
+        self.assertRaisesRegex(AssertionError,
+                     r"Expected 'geoffrey' to be called once\.",
+                     mock.assert_called_once_with)
+
+
     def test__name__(self):
         mock = Mock()
         self.assertRaises(AttributeError, lambda: mock.__name__)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,8 @@
 Library
 -------
 
+- Issue #15323: improve failure message of Mock.assert_called_once_with
+
 - Issue #16064: unittest -m claims executable is "python", not "python3"
 
 - Issue #12376: Pass on parameters in TextTestResult.__init__ super call

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list