summaryrefslogtreecommitdiff
path: root/mock/tests/testmock.py
diff options
context:
space:
mode:
authorPetter Strandmark <petter.strandmark@gmail.com>2018-10-28 21:37:10 +0100
committerChris Withers <chris@withers.org>2019-04-30 08:39:55 +0100
commit57ef3bb5a48ccc549563c42f4a3f1f105ab49c29 (patch)
tree45002705c376552d8ffb8380a8d78be469731477 /mock/tests/testmock.py
parent6065cab480b753bde660963dc122830e82ebb8ec (diff)
downloadmock-57ef3bb5a48ccc549563c42f4a3f1f105ab49c29.tar.gz
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. Backports: 47d94241a383e2b8a2c40e81d12d40d5947fb170 Signed-off-by: Chris Withers <chris@simplistix.co.uk>
Diffstat (limited to 'mock/tests/testmock.py')
-rw-r--r--mock/tests/testmock.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/mock/tests/testmock.py b/mock/tests/testmock.py
index 2f2c4c2..9b4ee7e 100644
--- a/mock/tests/testmock.py
+++ b/mock/tests/testmock.py
@@ -4,6 +4,7 @@
import copy
import pickle
+import re
import sys
import tempfile
@@ -432,6 +433,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):
@@ -1314,6 +1323,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):
@@ -1335,6 +1351,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()