]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
`unittest.mock` test and coverage fixup (#130787)
authorChris Withers <chris@withers.org>
Mon, 3 Mar 2025 11:44:59 +0000 (11:44 +0000)
committerGitHub <noreply@github.com>
Mon, 3 Mar 2025 11:44:59 +0000 (11:44 +0000)
* Mark functions that will never be called with # pragma: no cover

* Fix testpatch.PatchTest.test_exit_idempotent

.stop() and __exit__ have subtly different code paths, so to really test __exit__ idempotency, we need to call it specifically twice.

Lib/test/test_unittest/testmock/testhelpers.py
Lib/test/test_unittest/testmock/testmock.py
Lib/test/test_unittest/testmock/testpatch.py

index 8d0f3ebc5cba884010c1f8fadd7a3f9d9a15dd61..d1e48bde982040e1ea55256dab8b521c3e8955c8 100644 (file)
@@ -1080,7 +1080,7 @@ class SpecSignatureTest(unittest.TestCase):
         class WithMethod:
             a: int
             def b(self) -> int:
-                return 1
+                return 1  # pragma: no cover
 
         for mock in [
             create_autospec(WithMethod, instance=True),
index 5d1bf4258afacd5baabb381b6865faabcac1fe47..386d53bf5a5c63a89622bfff84cc88b26d617511 100644 (file)
@@ -316,7 +316,7 @@ class MockTest(unittest.TestCase):
         passed to the wrapped object and the return_value is returned instead.
         """
         def my_func():
-            return None
+            return None  # pragma: no cover
         func_mock = create_autospec(spec=my_func, wraps=my_func)
         return_value = "explicit return value"
         func_mock.return_value = return_value
index 7c5fc3deed2ca2dc5812f73aec0fa38eed438666..bd85fdcfc472a615e3cb15c678ddeea499962113 100644 (file)
@@ -748,7 +748,7 @@ class PatchTest(unittest.TestCase):
     def test_exit_idempotent(self):
         patcher = patch(foo_name, 'bar', 3)
         with patcher:
-            patcher.stop()
+            patcher.__exit__(None, None, None)
 
 
     def test_second_start_failure(self):