[Python-checkins] Add test for double patching instance methods (GH11126)

Chris Withers webhook-mailer at python.org
Wed Dec 12 04:00:47 EST 2018


https://github.com/python/cpython/commit/f27f0d2be4887f1a2c276d05246ab005964031d4
commit: f27f0d2be4887f1a2c276d05246ab005964031d4
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Chris Withers <chris at withers.org>
date: 2018-12-12T09:00:44Z
summary:

Add test for double patching instance methods (GH11126)

(cherry picked from commit 5a718e918db6211b633a7afb2bf537eb5b56cb1b)

Co-authored-by: Anthony Sottile <asottile at umich.edu>

files:
A Misc/NEWS.d/next/Tests/2018-12-10-13-18-37.bpo-26704.DBAN4c.rst
M Lib/unittest/test/testmock/testwith.py

diff --git a/Lib/unittest/test/testmock/testwith.py b/Lib/unittest/test/testmock/testwith.py
index 43b36a119952..ec4e540dcfd9 100644
--- a/Lib/unittest/test/testmock/testwith.py
+++ b/Lib/unittest/test/testmock/testwith.py
@@ -126,6 +126,20 @@ def test_dict_context_manager(self):
 
         self.assertEqual(foo, {})
 
+    def test_double_patch_instance_method(self):
+        class C:
+            def f(self):
+                pass
+
+        c = C()
+
+        with patch.object(c, 'f', autospec=True) as patch1:
+            with patch.object(c, 'f', autospec=True) as patch2:
+                c.f()
+            self.assertEqual(patch2.call_count, 1)
+            self.assertEqual(patch1.call_count, 0)
+            c.f()
+        self.assertEqual(patch1.call_count, 1)
 
 
 class TestMockOpen(unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Tests/2018-12-10-13-18-37.bpo-26704.DBAN4c.rst b/Misc/NEWS.d/next/Tests/2018-12-10-13-18-37.bpo-26704.DBAN4c.rst
new file mode 100644
index 000000000000..458f495be483
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2018-12-10-13-18-37.bpo-26704.DBAN4c.rst
@@ -0,0 +1,2 @@
+Added test demonstrating double-patching of an instance method.  Patch by
+Anthony Sottile.



More information about the Python-checkins mailing list