diff --git a/changelog.d/1142.fixed.rst b/changelog.d/1142.fixed.rst new file mode 100644 index 00000000..40a917ec --- /dev/null +++ b/changelog.d/1142.fixed.rst @@ -0,0 +1 @@ +The deprecation warning for an unset `asyncio_default_fixture_loop_scope` is now visible to pytest: it appears in the warnings summary and can be filtered with `-W` and `filterwarnings`. diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 38b75e41..3060aae5 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -297,7 +297,9 @@ def pytest_configure(config: Config) -> None: 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)) + config.issue_config_time_warning( + PytestDeprecationWarning(_DEFAULT_FIXTURE_LOOP_SCOPE_UNSET), stacklevel=2 + ) default_test_loop_scope = config.getini("asyncio_default_test_loop_scope") _validate_scope(default_test_loop_scope, "asyncio_default_test_loop_scope") diff --git a/tests/test_fixture_loop_scopes.py b/tests/test_fixture_loop_scopes.py index a25e56a0..6b4f6707 100644 --- a/tests/test_fixture_loop_scopes.py +++ b/tests/test_fixture_loop_scopes.py @@ -113,6 +113,54 @@ async def test_runs_in_fixture_loop(fixture_loop): result.assert_outcomes(passed=1) +_UNSET_FIXTURE_LOOP_SCOPE_WARNING = ( + "*PytestDeprecationWarning: The configuration option " + '"asyncio_default_fixture_loop_scope" is unset.*' +) + + +def test_unset_default_fixture_loop_scope_warning_appears_in_summary( + pytester: Pytester, +): + """ + The warning is emitted during configure, so it must still be recorded. + + https://github.com/pytest-dev/pytest-asyncio/issues/1142 + """ + pytester.makepyfile("async def test_it(): pass") + result = pytester.runpytest("--asyncio-mode=auto") + result.assert_outcomes(passed=1, warnings=1) + result.stdout.fnmatch_lines( + ["*warnings summary*", _UNSET_FIXTURE_LOOP_SCOPE_WARNING] + ) + + +@pytest.mark.parametrize( + ("ini", "args"), + ( + pytest.param( + "", ("-Wignore::pytest.PytestDeprecationWarning",), id="command-line-W" + ), + pytest.param( + "filterwarnings = ignore::pytest.PytestDeprecationWarning", + (), + id="filterwarnings-ini", + ), + ), +) +def test_unset_default_fixture_loop_scope_warning_is_filterable( + pytester: Pytester, + ini: str, + args: tuple[str, ...], +): + """Being recorded properly also means users can silence it.""" + pytester.makeini(f"[pytest]\n{ini}") + pytester.makepyfile("async def test_it(): pass") + result = pytester.runpytest("--asyncio-mode=auto", *args) + result.assert_outcomes(passed=1, warnings=0) + result.stdout.no_fnmatch_line(_UNSET_FIXTURE_LOOP_SCOPE_WARNING) + + def test_invalid_default_fixture_loop_scope_raises_error(pytester: Pytester): pytester.makeini("""\ [pytest]