summaryrefslogtreecommitdiffstats
path: root/Lib/unittest
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-09-17 11:35:56 (GMT)
committerGitHub <noreply@github.com>2019-09-17 11:35:56 (GMT)
commitf668d2b775da4bcd07e142c4bc5ebd88165fadf4 (patch)
treed9abc9d44d2277ad579cd24d7259a24137c9dd0f /Lib/unittest
parent728bea60e5ff2bea36f8f9a3cafe6b96e452c211 (diff)
downloadcpython-f668d2b775da4bcd07e142c4bc5ebd88165fadf4.zip
cpython-f668d2b775da4bcd07e142c4bc5ebd88165fadf4.tar.gz
cpython-f668d2b775da4bcd07e142c4bc5ebd88165fadf4.tar.bz2
bpo-37828: Fix default mock_name in unittest.mock.assert_called error (GH-16166)
In the format string for assert_called the evaluation order is incorrect and hence for mock's without name, 'None' is printed whereas it should be 'mock' like for other messages. The error message is ("Expected '%s' to have been called." % self._mock_name or 'mock'). (cherry picked from commit 5f5f11faf9de0d8dcbe1a8a4eb35d2a4232d6eaa) Co-authored-by: Abraham Toriz Cruz <awonderfulcode@gmail.com>
Diffstat (limited to 'Lib/unittest')
-rw-r--r--Lib/unittest/mock.py2
-rw-r--r--Lib/unittest/test/testmock/testmock.py8
2 files changed, 9 insertions, 1 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 497aa6f..9fd5c3c 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -873,7 +873,7 @@ class NonCallableMock(Base):
"""
if self.call_count == 0:
msg = ("Expected '%s' to have been called." %
- self._mock_name or 'mock')
+ (self._mock_name or 'mock'))
raise AssertionError(msg)
def assert_called_once(self):
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index 413ee68..2847d65 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -388,6 +388,14 @@ class MockTest(unittest.TestCase):
_check(mock)
+ def test_assert_called_exception_message(self):
+ msg = "Expected '{0}' to have been called"
+ with self.assertRaisesRegex(AssertionError, msg.format('mock')):
+ Mock().assert_called()
+ with self.assertRaisesRegex(AssertionError, msg.format('test_name')):
+ Mock(name="test_name").assert_called()
+
+
def test_assert_called_once_with(self):
mock = Mock()
mock()