summaryrefslogtreecommitdiff
path: root/Lib/unittest/mock.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2016-03-11 22:17:48 +0100
committerVictor Stinner <victor.stinner@gmail.com>2016-03-11 22:17:48 +0100
commit2c2a4e63d794eb55e9163322ea11b9765e9e0db5 (patch)
tree220e3dd3c738935ee81a2e80fa733e4f650c07a9 /Lib/unittest/mock.py
parent82442b7022fbc438983eb4e973418357e3eec1e2 (diff)
downloadcpython-git-2c2a4e63d794eb55e9163322ea11b9765e9e0db5.tar.gz
Add Mock.assert_called()
Issue #26323: Add assert_called() and assert_called_once() methods to unittest.mock.Mock.
Diffstat (limited to 'Lib/unittest/mock.py')
-rw-r--r--Lib/unittest/mock.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 21f49fab1b..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.