summaryrefslogtreecommitdiff
path: root/Lib/unittest
diff options
context:
space:
mode:
authorRobert Collins <rbtcollins@hp.com>2016-03-15 13:33:28 +1300
committerRobert Collins <rbtcollins@hp.com>2016-03-15 13:33:28 +1300
commit5e70966f1d8dbdde6324317fb37e3b2d3bfcfff5 (patch)
tree4a5dfce2c6aeec75baf128a4f440aaf68b121db9 /Lib/unittest
parent81074276bd41a87d82c35eff6f68a655b73f6847 (diff)
parent450fd5d935b0c187e2a39624a42df0acfc3c4d77 (diff)
downloadcpython-5e70966f1d8dbdde6324317fb37e3b2d3bfcfff5.tar.gz
#25320: Handle sockets in directories unittest discovery is scanning.
Patch from Victor van den Elzen.
Diffstat (limited to 'Lib/unittest')
-rw-r--r--Lib/unittest/mock.py20
-rw-r--r--Lib/unittest/test/testmock/testmock.py21
2 files changed, 40 insertions, 1 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 976f663c0a..50ff949c90 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -772,6 +772,24 @@ class NonCallableMock(Base):
(self._mock_name or 'mock', self.call_count))
raise AssertionError(msg)
+ def assert_called(_mock_self):
+ """assert that the mock was called at least once
+ """
+ self = _mock_self
+ if self.call_count == 0:
+ msg = ("Expected '%s' to have been called." %
+ self._mock_name or 'mock')
+ raise AssertionError(msg)
+
+ def assert_called_once(_mock_self):
+ """assert that the mock was called only once.
+ """
+ self = _mock_self
+ if not self.call_count == 1:
+ msg = ("Expected '%s' to have been called once. Called %s times." %
+ (self._mock_name or 'mock', self.call_count))
+ raise AssertionError(msg)
+
def assert_called_with(_mock_self, *args, **kwargs):
"""assert that the mock was called with the specified arguments.
@@ -820,7 +838,7 @@ class NonCallableMock(Base):
if expected not in all_calls:
raise AssertionError(
'Calls not found.\nExpected: %r\n'
- 'Actual: %r' % (calls, self.mock_calls)
+ 'Actual: %r' % (_CallList(calls), self.mock_calls)
) from cause
return
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index 2a6069f66a..b2d9acbc12 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -1222,6 +1222,27 @@ class MockTest(unittest.TestCase):
with self.assertRaises(AssertionError):
m.hello.assert_not_called()
+ def test_assert_called(self):
+ m = Mock()
+ with self.assertRaises(AssertionError):
+ m.hello.assert_called()
+ m.hello()
+ m.hello.assert_called()
+
+ m.hello()
+ m.hello.assert_called()
+
+ def test_assert_called_once(self):
+ m = Mock()
+ with self.assertRaises(AssertionError):
+ m.hello.assert_called_once()
+ m.hello()
+ m.hello.assert_called_once()
+
+ m.hello()
+ with self.assertRaises(AssertionError):
+ m.hello.assert_called_once()
+
#Issue21256 printout of keyword args should be in deterministic order
def test_sorted_call_signature(self):
m = Mock()