Skip to content

fix: validate asyncio_mode in pytest_configure - #1639

Open
saruk-vedant wants to merge 2 commits into
pytest-dev:mainfrom
saruk-vedant:pytest-asyncio-1633-asyncio-mode-validation
Open

saruk-vedant wants to merge 2 commits into
pytest-dev:mainfrom
saruk-vedant:pytest-asyncio-1633-asyncio-mode-validation

Conversation

@saruk-vedant

Copy link
Copy Markdown

Summary

asyncio_mode was only validated by _get_asyncio_mode(), which first runs from pytest_report_header. With -q the header is suppressed, so an invalid value was silently accepted when no async test was collected (a plain 1 passed run), and only surfaced later as a per-test error when an async test was present.

This PR calls _get_asyncio_mode(config) in pytest_configure, so an invalid asyncio_mode now raises the same single usage error (exit code 4) in all cases, matching how asyncio_default_fixture_loop_scope is already validated there.

Also adds a changelog fragment (changelog.d/1633.fixed.rst).

How to test

  • pytest -o asyncio_mode=bogus (non-quiet): exit 4, ERROR: 'bogus' is not a valid asyncio_mode. Valid modes: auto, strict.
  • pytest -q -o asyncio_mode=bogus test_sync.py (sync-only tests): previously 1 passed, now exit 4 with the same error.
  • pytest -q -o asyncio_mode=bogus test_async.py: previously a per-test setup error, now exit 4 with the same single error.
  • pytest -q --asyncio-mode=bogus and a bogus value in the ini file: exit 4 with the same error.
  • Valid values (auto, strict) and the default (no setting) still pass in quiet and non-quiet runs.
  • An invalid asyncio_default_fixture_loop_scope still raises its own usage error when the mode is valid.

Verification

  • New regression tests in tests/test_simple.py: test_invalid_asyncio_mode_quiet (parametrized over the -o ini override and the --asyncio-mode option) and test_invalid_asyncio_mode_quiet_with_async_test.
  • pytest tests/test_simple.py tests/modes/ -q: 22 passed, 3 skipped.
  • Full suite pytest tests/ -q: 263 passed, 3 skipped, 1 failed. The single failure (tests/test_set_event_loop.py::test_asyncio_run_after_async_fixture_does_not_leak_loop) is pre-existing: it fails identically on the unmodified tree (a PytestAssertRewriteWarning raised as an error in the inner pytester run, unrelated to this change).
  • ruff check (repo-pinned 0.14.13) on the changed files: all checks passed. ruff format on the changed files flags only pre-existing hunks. pyright pytest_asyncio/ tests/: 0 errors.

Fixes #1633

Call _get_asyncio_mode in pytest_configure so an invalid asyncio_mode raises a usage error at configuration time, even when -q suppresses the report header where the mode was previously first read.

Fixes pytest-dev#1633
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.87%. Comparing base (94366a2) to head (b2c3103).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1639      +/-   ##
==========================================
+ Coverage   94.86%   94.87%   +0.01%     
==========================================
  Files           2        2              
  Lines         506      507       +1     
  Branches       60       60              
==========================================
+ Hits          480      481       +1     
  Misses         21       21              
  Partials        5        5              

☔ 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.

Invalid asyncio_mode is only validated when the report header is printed

2 participants