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
56 changes: 47 additions & 9 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,22 +741,60 @@ def pytest_pycollect_makeitem_convert_async_functions_to_subclass(
hook_result.force_result(updated_node_collection)


_SCOPE_ORDER: dict[str, int] = {
"session": 0,
"package": 1,
"module": 2,
"class": 3,
"function": 4,
}


def _sync_test_async_fixture_loop_scope(metafunc: pytest.Metafunc) -> _ScopeName | None:
asyncio_mode = _get_asyncio_mode(metafunc.config)
default_fixture_loop_scope = metafunc.config.getini(
"asyncio_default_fixture_loop_scope"
)
scopes: list[_ScopeName] = []
for fixtures in metafunc.definition._fixtureinfo.name2fixturedefs.values():
fixturedef = fixtures[-1]
if _is_asyncio_fixture_function(fixturedef.func) or (
asyncio_mode == Mode.AUTO and _is_coroutine_or_asyncgen(fixturedef.func)
):
fallback = (
fixturedef.scope
if default_fixture_loop_scope == _MATCH_FIXTURE_SCOPE
else (default_fixture_loop_scope or fixturedef.scope)
)
scopes.append(getattr(fixturedef.func, "_loop_scope", None) or fallback)
if not scopes:
return None
return min(scopes, key=lambda s: _SCOPE_ORDER.get(s, 4))


@pytest.hookimpl(tryfirst=True)
def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
specialized_item_class = PytestAsyncioFunction.item_subclass_for(
metafunc.definition
)
sync_fixture_loop_scope: _ScopeName | None = None
if specialized_item_class is None:
return
sync_fixture_loop_scope = _sync_test_async_fixture_loop_scope(metafunc)
if sync_fixture_loop_scope is None:
return
marker_loop_scope = None
marker_selected_factory_names = None
else:
asyncio_marker = _resolve_asyncio_marker(metafunc.definition)
if asyncio_marker is None:
return
nodeid = metafunc.definition.nodeid
marker_loop_scope, marker_selected_factory_names = _parse_asyncio_marker(
asyncio_marker,
nodeid,
)

asyncio_marker = _resolve_asyncio_marker(metafunc.definition)
if asyncio_marker is None:
return
nodeid = metafunc.definition.nodeid
marker_loop_scope, marker_selected_factory_names = _parse_asyncio_marker(
asyncio_marker,
nodeid,
)

hook_factories = _collect_hook_loop_factories(metafunc.config, metafunc.definition)
if hook_factories is None:
Expand Down Expand Up @@ -796,7 +834,7 @@ def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
]
metafunc.fixturenames.append(_asyncio_loop_factory.__name__)
default_loop_scope = _get_default_test_loop_scope(metafunc.config)
loop_scope = marker_loop_scope or default_loop_scope
loop_scope = sync_fixture_loop_scope or marker_loop_scope or default_loop_scope
hide_id = len(factory_ids) == 1
metafunc.parametrize(
_asyncio_loop_factory.__name__,
Expand Down
33 changes: 33 additions & 0 deletions tests/test_loop_factory_parametrization.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,3 +917,36 @@ async def test_fixture_matches_running_loop(fixture_loop_type):
"""))
result = pytester.runpytest("--asyncio-mode=strict", "-v")
result.assert_outcomes(passed=2)


def test_sync_test_with_shared_async_fixture_and_loop_factories(
pytester: Pytester,
) -> None:
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = session")
pytester.makeconftest(dedent("""\
import asyncio

def pytest_asyncio_loop_factories(config, item):
return {"default": asyncio.new_event_loop}
"""))
pytester.makepyfile(dedent("""\
import pytest
import pytest_asyncio

@pytest_asyncio.fixture(scope="session")
async def parent():
yield "parent"

@pytest_asyncio.fixture(scope="session")
async def child(parent):
yield "child"

@pytest.mark.asyncio(loop_scope="session")
async def test_async(parent):
assert parent == "parent"

def test_sync(child):
assert child == "child"
"""))
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=2)
Loading