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")