Skip to content

Fix loop factories being ignored when asyncio marker comes from parametrization - #1631

Open
tony1233200 wants to merge 2 commits into
pytest-dev:mainfrom
tony1233200:fix-1463-loop-factories-parametrized-markers
Open

tony1233200 wants to merge 2 commits into
pytest-dev:mainfrom
tony1233200:fix-1463-loop-factories-parametrized-markers

Conversation

@tony1233200

Copy link
Copy Markdown

Fixes #1463.

Problem

When pytest.mark.asyncio is attached to individual parameter sets -- via pytest.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 configured pytest_asyncio_loop_factories were silently ignored and the test ran on the default event loop. pytest_generate_tests only 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_tests hook 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:

  • single factory: the asyncio parameter set runs on the configured loop, other sets run exactly once as before;
  • several factories: the asyncio parameter set runs once per factory, other sets are still run only once (verified: test_async[asyncio-a], test_async[asyncio-b], test_async[trio]);
  • a loop_factories selection carried by a parametrized marker is honored;
  • an invalid marker on one parameter set (e.g. an unknown keyword argument) is left to the existing per-item validation at setup time, so the valid parameter sets still run instead of the whole collection failing.

This works around the lack of a public pytest API for dependent parametrization (pytest-dev/pytest#13233): metafunc.parametrize can 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

  • The exact repro from pytest_asyncio_loop_factories hook is not called when pytest.mark.asyncio is added via parametrization #1463 (uvloop + trio) failed before the fix (asyncio set ran on the default loop) and passes after it.
  • Added 4 regression tests in tests/test_loop_factory_parametrization.py covering the parametrize case, the multi-factory no-duplication case, the fixture-params case, and a loop_factories selection via a parametrized marker.
  • Full suite: 259 passed, 3 skipped; the 6 remaining failures are pre-existing on the unmodified tree in this environment (5 hypothesis tests fail only because hypothesis is not installed here; test_asyncio_run_after_async_fixture_does_not_leak_loop fails identically without this change).
  • ruff check clean except one pre-existing finding on the unmodified code; mypy clean.

…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-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.20690% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.96%. Comparing base (599fb32) to head (b6c6761).

Files with missing lines Patch % Lines
pytest_asyncio/plugin.py 86.20% 4 Missing and 4 partials ⚠️
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.
📢 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.

This branch has not been deployed

No deployments
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.

pytest_asyncio_loop_factories hook is not called when pytest.mark.asyncio is added via parametrization

3 participants