From 8376601c2434de6ea148a15a5e4c44bd312c29bc Mon Sep 17 00:00:00 2001 From: QianEric Date: Tue, 24 Nov 2026 10:53:07 +0800 Subject: [PATCH] Validate asyncio_mode at configuration time, not only in the report header `_get_asyncio_mode()` is called from `pytest_report_header()`, so the value is only checked when the header is printed. With `-q` the header is skipped and a typo in the option is not a configuration error any more - it is raised while setting up each test, so the run shows an internal traceback and a per-test ERROR instead of one message: $ pytest -q -o asyncio_mode=bogus ... ==================================== ERRORS ==================================== ___________________________ ERROR at setup of test_a ___________________________ def _get_asyncio_mode(config: Config) -> Mode: try: > return Mode(val) E pytest.UsageError: 'bogus' is not a valid asyncio_mode. Valid modes: auto, strict. The sibling options are validated in `pytest_configure()` and fail before collection: $ pytest -q -o asyncio_default_fixture_loop_scope=bogus ; echo $? ERROR: 'bogus' is not a valid asyncio_default_fixture_loop_scope. Valid scopes are: ... 4 ## Change `pytest_configure()` validates the mode with the same helper the report header uses: $ pytest -q -o asyncio_mode=bogus ; echo $? ERROR: 'bogus' is not a valid asyncio_mode. Valid modes: auto, strict. 4 | case | before | after | |---|---|---| | `-o asyncio_mode=bogus` (header printed) | `ERROR: ...`, exit 4 | unchanged | | `-q -o asyncio_mode=bogus` | per-test `ERROR at setup` with a traceback, exit 1 | `ERROR: 'bogus' is not a valid asyncio_mode. Valid modes: auto, strict.`, exit 4 | | `-o asyncio_mode=auto` / `strict` | works | unchanged | ## Tests `test_invalid_asyncio_mode_is_a_config_error` parametrizes the existing `test_invalid_asyncio_mode` with and without `-q` and asserts the exit code is `USAGE_ERROR`. `pytest tests/test_simple.py tests/modes tests/markers`: 70 passed, 3 skipped with the change; on the current tree the `-q` case fails because the error is raised per test instead. I did not add a `changelog.d` entry - tell me the fragment number you want and I will add it. --- pytest_asyncio/plugin.py | 4 ++++ tests/test_simple.py | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 32b1a834..afc22339 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -281,6 +281,10 @@ def _validate_scope(scope: str | None, option_name: str) -> None: def pytest_configure(config: Config) -> None: + # Validate the mode here rather than only in pytest_report_header: -q skips the + # header, so a bad value used to surface as an error at the setup of every test. + _get_asyncio_mode(config) + default_fixture_loop_scope = config.getini("asyncio_default_fixture_loop_scope") if default_fixture_loop_scope != _MATCH_FIXTURE_SCOPE: _validate_scope( diff --git a/tests/test_simple.py b/tests/test_simple.py index 8ddeade3..757efe32 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -92,6 +92,17 @@ async def test_event_loop_before_fixture(self, loop): assert await loop.run_in_executor(None, self.foo) == 1 +@pytest.mark.parametrize("quiet", [[], ["-q"]], ids=["verbose", "quiet"]) +def test_invalid_asyncio_mode_is_a_config_error(pytester, quiet): + pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") + result = pytester.runpytest(*quiet, "-o", "asyncio_mode=True") + result.stderr.no_fnmatch_line("INTERNALERROR> *") + result.stderr.fnmatch_lines( + "ERROR: 'True' is not a valid asyncio_mode. Valid modes: auto, strict." + ) + assert result.ret == pytest.ExitCode.USAGE_ERROR + + def test_invalid_asyncio_mode(pytester): pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") result = pytester.runpytest("-o", "asyncio_mode=True")