From d9878b01f145d961e0c7895a825cae79c502c1eb Mon Sep 17 00:00:00 2001 From: Vedant saruk Date: Fri, 27 Nov 2026 08:59:07 +0530 Subject: [PATCH 1/2] fix: validate asyncio_mode in pytest_configure 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 #1633 --- changelog.d/1633.fixed.rst | 1 + pytest_asyncio/plugin.py | 4 ++++ tests/test_simple.py | 44 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 changelog.d/1633.fixed.rst diff --git a/changelog.d/1633.fixed.rst b/changelog.d/1633.fixed.rst new file mode 100644 index 00000000..b227daaf --- /dev/null +++ b/changelog.d/1633.fixed.rst @@ -0,0 +1 @@ +An invalid ``asyncio_mode`` is now reported during configuration, instead of being silently accepted when pytest runs with ``-q``. diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 32b1a834..15221ab3 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 eagerly so that an invalid value fails here, + # even when `-q` suppresses the report header (where the mode + # would otherwise first be read). + _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..6f296eb4 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -99,3 +99,47 @@ def test_invalid_asyncio_mode(pytester): result.stderr.fnmatch_lines( "ERROR: 'True' is not a valid asyncio_mode. Valid modes: auto, strict." ) + + +@pytest.mark.parametrize( + "mode_args", + [ + ["-o", "asyncio_mode=bogus"], + ["--asyncio-mode=bogus"], + ], +) +def test_invalid_asyncio_mode_quiet(pytester, mode_args): + """ + Invalid asyncio_mode errors in quiet mode with sync-only tests. + + With `-q` the report header is suppressed, so the mode used to be + silently accepted when no async test was collected. + """ + pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") + pytester.makepyfile("def test_sync():\n pass\n") + result = pytester.runpytest("-q", *mode_args) + assert result.ret == 4 + result.stderr.no_fnmatch_line("INTERNALERROR> *") + result.stderr.fnmatch_lines( + "ERROR: 'bogus' is not a valid asyncio_mode. Valid modes: auto, strict." + ) + + +def test_invalid_asyncio_mode_quiet_with_async_test(pytester): + """Invalid asyncio_mode fails at configuration time, not per test.""" + pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") + pytester.makepyfile( + dedent("""\ + import pytest + + @pytest.mark.asyncio + async def test_async(): + pass + """) + ) + result = pytester.runpytest("-q", "-o", "asyncio_mode=bogus") + assert result.ret == 4 + result.stderr.no_fnmatch_line("INTERNALERROR> *") + result.stderr.fnmatch_lines( + "ERROR: 'bogus' is not a valid asyncio_mode. Valid modes: auto, strict." + ) From b2c310324afdc2da6da94009b27bbbb844d73929 Mon Sep 17 00:00:00 2001 From: Vedant saruk Date: Fri, 27 Nov 2026 10:29:16 +0530 Subject: [PATCH 2/2] style: apply shed formatting in quiet-mode async test --- tests/test_simple.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_simple.py b/tests/test_simple.py index 6f296eb4..2dad4e46 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -128,15 +128,13 @@ def test_invalid_asyncio_mode_quiet(pytester, mode_args): def test_invalid_asyncio_mode_quiet_with_async_test(pytester): """Invalid asyncio_mode fails at configuration time, not per test.""" pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") - pytester.makepyfile( - dedent("""\ + pytester.makepyfile(dedent("""\ import pytest @pytest.mark.asyncio async def test_async(): pass - """) - ) + """)) result = pytester.runpytest("-q", "-o", "asyncio_mode=bogus") assert result.ret == 4 result.stderr.no_fnmatch_line("INTERNALERROR> *")