summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Foord <michael@voidspace.org.uk>2012-09-28 15:15:22 (GMT)
committerMichael Foord <michael@voidspace.org.uk>2012-09-28 15:15:22 (GMT)
commit28d591ceef337cc37c4b44d7958dea3f59d92018 (patch)
tree41b989c914e0a0b5e78b37dd9d19319e3284246b
parent494502756eb7283631930c18ccaa6b137959d961 (diff)
downloadcpython-28d591ceef337cc37c4b44d7958dea3f59d92018.zip
cpython-28d591ceef337cc37c4b44d7958dea3f59d92018.tar.gz
cpython-28d591ceef337cc37c4b44d7958dea3f59d92018.tar.bz2
Closes issue 15323. Improve failure message of Mock.assert_called_once_with
-rw-r--r--Doc/library/unittest.mock.rst4
-rw-r--r--Lib/unittest/mock.py4
-rw-r--r--Lib/unittest/test/testmock/testmock.py7
-rw-r--r--Misc/NEWS2
4 files changed, 13 insertions, 4 deletions
diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst
index bed698a..3e50031 100644
--- a/Doc/library/unittest.mock.rst
+++ b/Doc/library/unittest.mock.rst
@@ -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
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 95570aa..324cf39 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -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)
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index 64fd1a1..2c6f128 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -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__)
diff --git a/Misc/NEWS b/Misc/NEWS
index c3fc686..43fcfa4 100644
--- 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