diff --git a/changelog.d/1189.added.rst b/changelog.d/1189.added.rst new file mode 100644 index 00000000..889feecf --- /dev/null +++ b/changelog.d/1189.added.rst @@ -0,0 +1 @@ +Raise a ``pytest.UsageError`` on invalid ``asyncio_default_fixture_loop_scope`` and ``asyncio_default_test_loop_scope`` configuration values. (`#1189 `_) diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index ec52ee4c..8eb99c9d 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -229,10 +229,25 @@ def _get_asyncio_debug(config: Config) -> bool: """ +def _validate_scope(scope: str | None, option_name: str) -> None: + if scope is None: + return + valid_scopes = [s.value for s in Scope] + if scope not in valid_scopes: + raise pytest.UsageError( + f"{scope!r} is not a valid {option_name}. " + f"Valid scopes are: {', '.join(valid_scopes)}." + ) + + def pytest_configure(config: Config) -> None: - default_loop_scope = config.getini("asyncio_default_fixture_loop_scope") - if not default_loop_scope: + default_fixture_loop_scope = config.getini("asyncio_default_fixture_loop_scope") + _validate_scope(default_fixture_loop_scope, "asyncio_default_fixture_loop_scope") + if not default_fixture_loop_scope: warnings.warn(PytestDeprecationWarning(_DEFAULT_FIXTURE_LOOP_SCOPE_UNSET)) + + default_test_loop_scope = config.getini("asyncio_default_test_loop_scope") + _validate_scope(default_test_loop_scope, "asyncio_default_test_loop_scope") config.addinivalue_line( "markers", "asyncio: " diff --git a/tests/test_fixture_loop_scopes.py b/tests/test_fixture_loop_scopes.py index a9ce4b35..95e46818 100644 --- a/tests/test_fixture_loop_scopes.py +++ b/tests/test_fixture_loop_scopes.py @@ -136,3 +136,20 @@ async def test_runs_in_fixture_loop(fixture_loop): ) result = pytester.runpytest_subprocess("--asyncio-mode=strict") result.assert_outcomes(passed=1) + + +def test_invalid_default_fixture_loop_scope_raises_error(pytester: Pytester): + pytester.makeini( + """\ + [pytest] + asyncio_default_fixture_loop_scope = invalid_scope + """ + ) + result = pytester.runpytest() + result.stderr.fnmatch_lines( + [ + "ERROR: 'invalid_scope' is not a valid " + "asyncio_default_fixture_loop_scope. Valid scopes are: " + "function, class, module, package, session." + ] + )