[Python-checkins] gh-103329: Add regression test for PropertyMock with side effect (#103358)

carljm webhook-mailer at python.org
Fri Apr 7 22:09:07 EDT 2023


https://github.com/python/cpython/commit/26c65980dc6d842879d133165bb7c461d98cc6c7
commit: 26c65980dc6d842879d133165bb7c461d98cc6c7
branch: main
author: Russell Keith-Magee <russell at keith-magee.com>
committer: carljm <carl at oddbird.net>
date: 2023-04-07T20:09:00-06:00
summary:

gh-103329: Add regression test for PropertyMock with side effect (#103358)

files:
A Misc/NEWS.d/next/Tests/2023-04-08-00-50-23.gh-issue-103329.M38tqF.rst
M Lib/test/test_unittest/testmock/testhelpers.py

diff --git a/Lib/test/test_unittest/testmock/testhelpers.py b/Lib/test/test_unittest/testmock/testhelpers.py
index 9e7ec5d62d5d..dc4d004cda8a 100644
--- a/Lib/test/test_unittest/testmock/testhelpers.py
+++ b/Lib/test/test_unittest/testmock/testhelpers.py
@@ -1077,7 +1077,7 @@ def test_propertymock(self):
             p.stop()
 
 
-    def test_propertymock_returnvalue(self):
+    def test_propertymock_bare(self):
         m = MagicMock()
         p = PropertyMock()
         type(m).foo = p
@@ -1088,6 +1088,27 @@ def test_propertymock_returnvalue(self):
         self.assertNotIsInstance(returned, PropertyMock)
 
 
+    def test_propertymock_returnvalue(self):
+        m = MagicMock()
+        p = PropertyMock(return_value=42)
+        type(m).foo = p
+
+        returned = m.foo
+        p.assert_called_once_with()
+        self.assertEqual(returned, 42)
+        self.assertNotIsInstance(returned, PropertyMock)
+
+
+    def test_propertymock_side_effect(self):
+        m = MagicMock()
+        p = PropertyMock(side_effect=ValueError)
+        type(m).foo = p
+
+        with self.assertRaises(ValueError):
+            m.foo
+        p.assert_called_once_with()
+
+
 class TestCallablePredicate(unittest.TestCase):
 
     def test_type(self):
diff --git a/Misc/NEWS.d/next/Tests/2023-04-08-00-50-23.gh-issue-103329.M38tqF.rst b/Misc/NEWS.d/next/Tests/2023-04-08-00-50-23.gh-issue-103329.M38tqF.rst
new file mode 100644
index 000000000000..79448ed72804
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2023-04-08-00-50-23.gh-issue-103329.M38tqF.rst
@@ -0,0 +1 @@
+Regression tests for the behaviour of ``unittest.mock.PropertyMock`` were added.



More information about the Python-checkins mailing list