From 8af9db3e4fa17fb7add3f904a19d816f7787ee1c Mon Sep 17 00:00:00 2001 From: Kushal Das Date: Thu, 17 Apr 2014 01:36:14 +0530 Subject: Closes Issue 21262: New method assert_not_called for Mock. It raises AssertionError if the mock has been called. --- Lib/unittest/mock.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'Lib/unittest/mock.py') diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 48e7dd078f2..6c00bc66f8f 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -758,6 +758,14 @@ class NonCallableMock(Base): else: return _call + def assert_not_called(_mock_self, *args, **kwargs): + """assert that the mock was never called. + """ + self = _mock_self + if self.call_count != 0: + msg = ("Expected '%s' to not have been called. 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. -- cgit v1.2.3