[Python-checkins] cpython (3.6): Fixes issue28380: unittest.mock Mock autospec functions now properly support

gregory.p.smith python-checkins at python.org
Thu Oct 6 17:32:27 EDT 2016


https://hg.python.org/cpython/rev/4e39b4e57673
changeset:   104341:4e39b4e57673
branch:      3.6
parent:      104339:b88be2133323
user:        Gregory P. Smith <greg at krypto.org>
date:        Thu Oct 06 14:31:23 2016 -0700
summary:
  Fixes issue28380: unittest.mock Mock autospec functions now properly support
assert_called, assert_not_called, and assert_called_once.

files:
  Lib/unittest/mock.py                    |  9 +++++++++
  Lib/unittest/test/testmock/testpatch.py |  6 ++++++
  Misc/NEWS                               |  3 +++
  3 files changed, 18 insertions(+), 0 deletions(-)


diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -193,6 +193,12 @@
 
     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 @@
     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
 
diff --git a/Lib/unittest/test/testmock/testpatch.py b/Lib/unittest/test/testmock/testpatch.py
--- a/Lib/unittest/test/testmock/testpatch.py
+++ b/Lib/unittest/test/testmock/testpatch.py
@@ -969,8 +969,14 @@
     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)
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -53,6 +53,9 @@
 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.

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


More information about the Python-checkins mailing list