Skip to content

test: regression test for #403 (mock.patch.dict + asyncio_mode=auto)#1443

Open
mvanhorn wants to merge 1 commit into
pytest-dev:mainfrom
mvanhorn:fix/test-auto-mode-mock-patch-dict
Open

test: regression test for #403 (mock.patch.dict + asyncio_mode=auto)#1443
mvanhorn wants to merge 1 commit into
pytest-dev:mainfrom
mvanhorn:fix/test-auto-mode-mock-patch-dict

Conversation

@mvanhorn
Copy link
Copy Markdown

Summary

#403 reported that async tests decorated with unittest.mock.patch.dict were silently skipped under asyncio_mode=auto, surfaced as a PytestUnhandledCoroutineWarning. The root cause was a CPython bug in how iscoroutinefunction unwrapped patch.dict. seifertm filed python/cpython#98086 and the fix was backported to CPython 3.10 (and 3.11). pytest-asyncio now requires Python >= 3.10, so the scenario is no longer reachable.

This PR adds a regression test in tests/modes/test_auto_mode.py to lock that property in. The test reproduces the issue's two failing variants - test_doesnt_run (only decorated with @mock.patch.dict) and test_runs_anyway (also explicitly marked @pytest.mark.asyncio) - and asserts both are collected and pass under --asyncio-mode=auto. If a future refactor reintroduces the unwrapping bug at this layer (e.g. switching back to a iscoroutinefunction call that doesn't see through patch.dict), this test fails immediately.

Files

  • tests/modes/test_auto_mode.py: new test_auto_mode_async_test_with_mock_patch_dict test
  • changelog.d/403.fixed.rst: news fragment

Verification

$ pytest tests/modes/test_auto_mode.py -v
...
tests/modes/test_auto_mode.py::test_auto_mode_cmdline PASSED             [ 14%]
tests/modes/test_auto_mode.py::test_auto_mode_cfg PASSED                 [ 28%]
tests/modes/test_auto_mode.py::test_auto_mode_async_fixture PASSED       [ 42%]
tests/modes/test_auto_mode.py::test_auto_mode_method_fixture PASSED      [ 57%]
tests/modes/test_auto_mode.py::test_auto_mode_static_method PASSED       [ 71%]
tests/modes/test_auto_mode.py::test_auto_mode_static_method_fixture PASSED [ 85%]
tests/modes/test_auto_mode.py::test_auto_mode_async_test_with_mock_patch_dict PASSED [100%]
============================== 7 passed in 0.11s ===============================

Also verified the original repro from the issue body runs cleanly under Python 3.14.3 + current pytest-asyncio main (3/3 passed), confirming the CPython backport is the right reason this test passes today rather than a workaround in this repo.

Closes #403

AI disclosure: regression test and PR body drafted with Claude as a coding assistant; I ran the tests and reviewed the changes before pushing.

…de=auto

pytest-dev#403 reported that async tests decorated with unittest.mock.patch.dict
were silently skipped under asyncio_mode=auto (PytestUnhandledCoroutine
Warning). The root cause was a CPython bug in iscoroutinefunction
unwrapping through patch.dict; the fix was backported to CPython 3.10.

pytest-asyncio currently requires Python >= 3.10, so the scenario is
no longer reachable. This adds a regression test in
tests/modes/test_auto_mode.py that asserts both the unmarked
auto-mode case and the explicitly-marked case (the issue's
test_doesnt_run and test_runs_anyway from the original repro) are
collected and pass.

Closes pytest-dev#403
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.50%. Comparing base (7582774) to head (5ec917f).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1443   +/-   ##
=======================================
  Coverage   94.50%   94.50%           
=======================================
  Files           2        2           
  Lines         510      510           
  Branches       62       62           
=======================================
  Hits          482      482           
  Misses         22       22           
  Partials        6        6           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Async test function gets skipped with asyncio_mode = auto and @unittest.mock.patch.dict decorator

2 participants