Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,24 @@ def _validate_scope(scope: str | None, option_name: str) -> None:
)


_warned_default_fixture_loop_scope_unset_key = pytest.StashKey[bool]()


def _warn_if_default_fixture_loop_scope_unset(config: Config) -> None:
if (
not config.getini("asyncio_default_fixture_loop_scope")
and not config.stash.get(_warned_default_fixture_loop_scope_unset_key, False)
):
config.stash[_warned_default_fixture_loop_scope_unset_key] = True
warnings.warn(PytestDeprecationWarning(_DEFAULT_FIXTURE_LOOP_SCOPE_UNSET))


def pytest_configure(config: Config) -> None:
default_fixture_loop_scope = config.getini("asyncio_default_fixture_loop_scope")
if default_fixture_loop_scope != _MATCH_FIXTURE_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")
Expand Down Expand Up @@ -861,6 +871,7 @@ def _set_event_loop(loop: AbstractEventLoop | None) -> None:
def pytest_pyfunc_call(pyfuncitem: Function) -> object | None:
"""Pytest hook called before a test case is run."""
if pyfuncitem.get_closest_marker("asyncio") is not None:
_warn_if_default_fixture_loop_scope_unset(pyfuncitem.config)
if is_async_test(pyfuncitem):
asyncio_mode = _get_asyncio_mode(pyfuncitem.config)
for fixname, fixtures in pyfuncitem._fixtureinfo.name2fixturedefs.items():
Expand Down Expand Up @@ -921,6 +932,7 @@ def pytest_fixture_setup(fixturedef: FixtureDef, request) -> object | None:
return (yield)
if not _is_coroutine_or_asyncgen(fixturedef.func):
return (yield)
_warn_if_default_fixture_loop_scope_unset(request.config)
default_loop_scope = request.config.getini("asyncio_default_fixture_loop_scope")
if default_loop_scope == _MATCH_FIXTURE_SCOPE:
default_loop_scope = fixturedef.scope
Expand Down
26 changes: 26 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,29 @@ def test_invalid_asyncio_mode(pytester):
result.stderr.fnmatch_lines(
"ERROR: 'True' is not a valid asyncio_mode. Valid modes: auto, strict."
)


def test_unset_default_fixture_loop_scope_no_warning_for_sync_suite(pytester):
pytester.makeini("[pytest]\n")
pytester.makepyfile("def test_sync(): pass")
result = pytester.runpytest_inprocess("-Werror")
assert result.ret == pytest.ExitCode.OK
result.assert_outcomes(passed=1)


def test_unset_default_fixture_loop_scope_warns_for_async_suite(pytester):
pytester.makeini("[pytest]\n")
pytester.makepyfile(
"""
import pytest

@pytest.mark.asyncio
async def test_async():
pass
"""
)
result = pytester.runpytest_inprocess("-Werror")
assert result.ret != pytest.ExitCode.OK
result.stdout.fnmatch_lines(
"*asyncio_default_fixture_loop_scope*is unset*"
)
Loading