]> granicus.if.org Git - python/commitdiff
Closes issue 15323. Improve failure message of Mock.assert_called_once_with
authorMichael Foord <michael@voidspace.org.uk>
Fri, 28 Sep 2012 15:15:22 +0000 (16:15 +0100)
committerMichael Foord <michael@voidspace.org.uk>
Fri, 28 Sep 2012 15:15:22 +0000 (16:15 +0100)
Doc/library/unittest.mock.rst
Lib/unittest/mock.py
Lib/unittest/test/testmock/testmock.py
Misc/NEWS

index bed698afe40b99f80ba1ed1191c58cc7e46d8ba7..3e500311158a3617bdfbc8bc7d3760971b4c67a6 100644 (file)
@@ -276,7 +276,7 @@ the `new_callable` argument to `patch`.
             >>> 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 @@ extremely handy: :meth:`~Mock.assert_called_with` and
     >>> 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
index 95570aa3a993f1f0b2fb2707cf7e8560a3c358c9..324cf399b37ad7d9a29e3af325048be540b618e1 100644 (file)
@@ -731,8 +731,8 @@ class NonCallableMock(Base):
         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)
 
index 64fd1a11c9bd4d2847923a84f20603babf2709e1..2c6f12897582d2b461259619c86f3904567efd7e 100644 (file)
@@ -463,6 +463,13 @@ class MockTest(unittest.TestCase):
                                 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__)
index c3fc68605b19ba503c2ccd534756ac8f83b87ddc..43fcfa42c76ceae316faafc2121c3a9ae781a52c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,8 @@ Core and Builtins
 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