Skip to content

Commit bf6f5a7

Browse files
committed
[feat] Detect multiple higher-scoped event loops requested in the same test.
Previously, the detection was limited to a function-scoped loop and a higher-scoped loop. Signed-off-by: Michael Seifert <[email protected]>
1 parent f233e5a commit bf6f5a7

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

Diff for: pytest_asyncio/plugin.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,19 @@ def pytest_generate_tests(metafunc: Metafunc) -> None:
715715
event_loop_fixture_id = event_loop_node.stash.get(_event_loop_fixture_id, None)
716716

717717
if event_loop_fixture_id:
718-
if "event_loop" in metafunc.fixturenames:
718+
collectors = _iter_collectors(metafunc.definition)
719+
collector_event_loop_fixture_ids = map(
720+
lambda c: c.stash.get(_event_loop_fixture_id, None), # type: ignore
721+
collectors,
722+
)
723+
possible_event_loop_fixture_ids = {"event_loop"} | set(
724+
collector_event_loop_fixture_ids
725+
)
726+
used_fixture_ids = {event_loop_fixture_id, *metafunc.fixturenames}
727+
used_event_loop_fixture_ids = possible_event_loop_fixture_ids.intersection(
728+
used_fixture_ids
729+
)
730+
if len(used_event_loop_fixture_ids) > 1:
719731
raise MultipleEventLoopsRequestedError(
720732
_MULTIPLE_LOOPS_REQUESTED_ERROR.format(
721733
test_name=metafunc.definition.nodeid,

Diff for: tests/async_fixtures/test_autouse_fixtures.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
from textwrap import dedent
22

3+
import pytest
34
from pytest import Pytester
45

56

7+
@pytest.mark.parametrize("autouse_fixture_scope", ("function", "module"))
68
def test_autouse_fixture_in_different_scope_triggers_multiple_event_loop_error(
79
pytester: Pytester,
10+
autouse_fixture_scope: str,
811
):
912
pytester.makepyfile(
1013
dedent(
11-
"""\
14+
f"""\
1215
import asyncio
1316
import pytest
1417
import pytest_asyncio
1518
1619
loop: asyncio.AbstractEventLoop
1720
18-
@pytest_asyncio.fixture(autouse=True)
21+
@pytest_asyncio.fixture(autouse=True, scope="{autouse_fixture_scope}")
1922
async def autouse_fixture():
2023
pass
2124

0 commit comments

Comments
 (0)