Repository navigation
Fix loop factories being ignored when asyncio marker comes from parametrization - #1631
Open
tony1233200 wants to merge 2 commits into
Open
tony1233200 wants to merge 2 commits into
tony1233200 wants to merge 2 commits into
Conversation
…etrization When pytest.mark.asyncio is attached to individual parameter sets (via pytest.param(..., marks=pytest.mark.asyncio) or via a parametrized fixture) instead of the test definition, pytest_generate_tests cannot see it, so the configured loop factories were never applied and the test ran on the default event loop. Add a wrapper hook that runs after pytest has processed the parametrization and parametrizes the loop factory fixture for exactly the calls carrying the asyncio marker, leaving other calls (e.g. ones run by pytest-trio) untouched. Fixes pytest-dev#1463
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1631 +/- ##
==========================================
- Coverage 94.86% 93.96% -0.91%
==========================================
Files 2 2
Lines 506 563 +57
Branches 60 75 +15
==========================================
+ Hits 480 529 +49
- Misses 21 25 +4
- Partials 5 9 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1463.
Problem
When
pytest.mark.asynciois attached to individual parameter sets -- viapytest.param(..., marks=pytest.mark.asyncio)or via a parametrized fixture (pytest.fixture(params=[pytest.param(..., marks=pytest.mark.asyncio)])) -- instead of the test definition, the configuredpytest_asyncio_loop_factorieswere silently ignored and the test ran on the default event loop.pytest_generate_testsonly sees the marker on the test definition, and at that point the per-parameter marks only exist on the individual callspecs once pytest has processed the parametrization.Fix
This adds a wrapper
pytest_generate_testshook that runs after pytest has processed the parametrization and parametrizes the loop-factory fixture for exactly the calls carrying the asyncio marker, leaving the other calls (e.g. ones run by pytest-trio) untouched:test_async[asyncio-a],test_async[asyncio-b],test_async[trio]);loop_factoriesselection carried by a parametrized marker is honored;This works around the lack of a public pytest API for dependent parametrization (pytest-dev/pytest#13233):
metafunc.parametrizecan only multiply all calls of a test function, so the loop-factory parametrization is applied to a temporarily narrowed-down call list and the results are spliced back into the full list.Verification
pytest_asyncio_loop_factorieshook is not called whenpytest.mark.asynciois added via parametrization #1463 (uvloop + trio) failed before the fix (asyncio set ran on the default loop) and passes after it.tests/test_loop_factory_parametrization.pycovering the parametrize case, the multi-factory no-duplication case, the fixture-params case, and aloop_factoriesselection via a parametrized marker.test_asyncio_run_after_async_fixture_does_not_leak_loopfails identically without this change).ruff checkclean except one pre-existing finding on the unmodified code;mypyclean.