[Python-checkins] cpython: Closes Issue 21262: New method assert_not_called for Mock.

kushal.das python-checkins at python.org
Wed Apr 16 22:06:23 CEST 2014


http://hg.python.org/cpython/rev/9e5cbc46e916
changeset:   90364:9e5cbc46e916
user:        Kushal Das <kushaldas at gmail.com>
date:        Thu Apr 17 01:36:14 2014 +0530
summary:
  Closes Issue 21262: New method assert_not_called for Mock.

It raises AssertionError if the mock has been called.

files:
  Doc/library/unittest.mock.rst          |  14 ++++++++++++++
  Lib/unittest/mock.py                   |   8 ++++++++
  Lib/unittest/test/testmock/testmock.py |   9 +++++++++
  Misc/NEWS                              |   3 +++
  4 files changed, 34 insertions(+), 0 deletions(-)


diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst
--- a/Doc/library/unittest.mock.rst
+++ b/Doc/library/unittest.mock.rst
@@ -321,6 +321,20 @@
             >>> calls = [call(4), call(2), call(3)]
             >>> mock.assert_has_calls(calls, any_order=True)
 
+    .. method:: assert_not_called(*args, **kwargs)
+
+        Assert the mock was never called.
+
+            >>> m = Mock()
+            >>> m.hello.assert_not_called()
+            >>> obj = m.hello()
+            >>> m.hello.assert_not_called()
+            Traceback (most recent call last):
+              ...
+            AssertionError: Expected 'hello' to not have been called. Called 1 times.
+
+        .. versionadded:: 3.5
+
 
     .. method:: reset_mock()
 
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -758,6 +758,14 @@
         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.
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -1198,6 +1198,15 @@
         m.assert_foo_call()
         m.assret_foo_call()
 
+    #Issue21262
+    def test_assert_not_called(self):
+        m = Mock()
+        m.hello.assert_not_called()
+        m.hello()
+        with self.assertRaises(AssertionError):
+            m.hello.assert_not_called()
+
+
     def test_mock_add_spec(self):
         class _One(object):
             one = 1
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -50,6 +50,9 @@
 Library
 -------
 
+- Issue #21262: New method assert_not_called for Mock.
+  It raises AssertionError if the mock has been called.
+
 - Issue #21238: New keyword argument `unsafe` to Mock. It raises
   `AttributeError` incase of an attribute startswith assert or assret.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list