]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fixes issue28380: unittest.mock Mock autospec functions now properly support
authorGregory P. Smith <greg@krypto.org>
Thu, 6 Oct 2016 21:31:23 +0000 (14:31 -0700)
committerGregory P. Smith <greg@krypto.org>
Thu, 6 Oct 2016 21:31:23 +0000 (14:31 -0700)
assert_called, assert_not_called, and assert_called_once.

Lib/unittest/mock.py
Lib/unittest/test/testmock/testpatch.py
Misc/NEWS

index eaa9c3d5850e33a7043651f6831ca7a2976fa52e..f1349198880c63c09769f13cb0e1a9be639a2682 100644 (file)
@@ -193,6 +193,12 @@ def _setup_func(funcopy, mock):
 
     def assert_called_with(*args, **kwargs):
         return mock.assert_called_with(*args, **kwargs)
+    def assert_called(*args, **kwargs):
+        return mock.assert_called(*args, **kwargs)
+    def assert_not_called(*args, **kwargs):
+        return mock.assert_not_called(*args, **kwargs)
+    def assert_called_once(*args, **kwargs):
+        return mock.assert_called_once(*args, **kwargs)
     def assert_called_once_with(*args, **kwargs):
         return mock.assert_called_once_with(*args, **kwargs)
     def assert_has_calls(*args, **kwargs):
@@ -223,6 +229,9 @@ def _setup_func(funcopy, mock):
     funcopy.assert_has_calls = assert_has_calls
     funcopy.assert_any_call = assert_any_call
     funcopy.reset_mock = reset_mock
+    funcopy.assert_called = assert_called
+    funcopy.assert_not_called = assert_not_called
+    funcopy.assert_called_once = assert_called_once
 
     mock._mock_delegate = funcopy
 
index 2e0c08f35f5226c7f2bcc108b5a06a3aab30534a..fe4ecefd44058a8c98869b494e557503fa6104a8 100644 (file)
@@ -969,8 +969,14 @@ class PatchTest(unittest.TestCase):
     def test_autospec_function(self):
         @patch('%s.function' % __name__, autospec=True)
         def test(mock):
+            function.assert_not_called()
+            self.assertRaises(AssertionError, function.assert_called)
+            self.assertRaises(AssertionError, function.assert_called_once)
             function(1)
+            self.assertRaises(AssertionError, function.assert_not_called)
             function.assert_called_with(1)
+            function.assert_called()
+            function.assert_called_once()
             function(2, 3)
             function.assert_called_with(2, 3)
 
index 5c5583ad85ac82454008d796f3498919594f686a..b03cc817f719480b822a40a826c241dd7a21229a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -53,6 +53,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #28380: unittest.mock Mock autospec functions now properly support
+  assert_called, assert_not_called, and assert_called_once.
+
 - Issue #27181 remove statistics.geometric_mean and defer until 3.7.
 
 - Issue #28229: lzma module now supports pathlib.