diff options
author | Petter Strandmark <petter.strandmark@gmail.com> | 2018-10-28 20:37:10 (GMT) |
---|---|---|
committer | Victor Stinner <vstinner@redhat.com> | 2018-10-28 20:37:10 (GMT) |
commit | 47d94241a383e2b8a2c40e81d12d40d5947fb170 (patch) | |
tree | b4c6d167ab2652014eb99815eb870d640341a82f /Lib/unittest/test | |
parent | 18032632ab27eed51d705c2be7b64bac708279bf (diff) | |
download | cpython-47d94241a383e2b8a2c40e81d12d40d5947fb170.zip cpython-47d94241a383e2b8a2c40e81d12d40d5947fb170.tar.gz cpython-47d94241a383e2b8a2c40e81d12d40d5947fb170.tar.bz2 |
bpo-35047, unittest.mock: Better error messages on assert_called_xxx failures (GH-10090)
unittest.mock now includes mock calls in exception messages if
assert_not_called, assert_called_once, or assert_called_once_with
fails.
Diffstat (limited to 'Lib/unittest/test')
-rw-r--r-- | Lib/unittest/test/testmock/testmock.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index c7bfa27..8cd284a 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1,4 +1,5 @@ import copy +import re import sys import tempfile @@ -407,6 +408,14 @@ class MockTest(unittest.TestCase): lambda: mock.assert_called_once_with('bob', 'bar', baz=2) ) + def test_assert_called_once_with_call_list(self): + m = Mock() + m(1) + m(2) + self.assertRaisesRegex(AssertionError, + re.escape("Calls: [call(1), call(2)]"), + lambda: m.assert_called_once_with(2)) + def test_assert_called_once_with_function_spec(self): def f(a, b, c, d=None): @@ -1250,6 +1259,13 @@ class MockTest(unittest.TestCase): with self.assertRaises(AssertionError): m.hello.assert_not_called() + def test_assert_not_called_message(self): + m = Mock() + m(1, 2) + self.assertRaisesRegex(AssertionError, + re.escape("Calls: [call(1, 2)]"), + m.assert_not_called) + def test_assert_called(self): m = Mock() with self.assertRaises(AssertionError): @@ -1271,6 +1287,20 @@ class MockTest(unittest.TestCase): with self.assertRaises(AssertionError): m.hello.assert_called_once() + def test_assert_called_once_message(self): + m = Mock() + m(1, 2) + m(3) + self.assertRaisesRegex(AssertionError, + re.escape("Calls: [call(1, 2), call(3)]"), + m.assert_called_once) + + def test_assert_called_once_message_not_called(self): + m = Mock() + with self.assertRaises(AssertionError) as e: + m.assert_called_once() + self.assertNotIn("Calls:", str(e.exception)) + #Issue21256 printout of keyword args should be in deterministic order def test_sorted_call_signature(self): m = Mock() |