Skip to content

Commit 8daa107

Browse files
committed
[test] Test the "loop_scope=session" argument to pytest_asyncio.fixture with additional pytest fixture scopes.
Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de>
1 parent 412a73e commit 8daa107

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

tests/test_fixture_loop_scopes.py

+29-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
1-
import asyncio
1+
from textwrap import dedent
22

33
import pytest
4+
from pytest import Pytester
45

5-
import pytest_asyncio
66

7-
loop: asyncio.AbstractEventLoop
7+
@pytest.mark.parametrize(
8+
"fixture_scope", ("session", "package", "module", "class", "function")
9+
)
10+
def test_loop_scope_session_is_independent_of_fixture_scope(
11+
pytester: Pytester,
12+
fixture_scope: str,
13+
):
14+
pytester.makepyfile(
15+
dedent(
16+
f"""\
17+
import asyncio
18+
import pytest
19+
import pytest_asyncio
820
21+
loop: asyncio.AbstractEventLoop = None
922
10-
@pytest_asyncio.fixture(loop_scope="session")
11-
async def fixture():
12-
global loop
13-
loop = asyncio.get_running_loop()
23+
@pytest_asyncio.fixture(scope="{fixture_scope}", loop_scope="session")
24+
async def fixture():
25+
global loop
26+
loop = asyncio.get_running_loop()
1427
15-
16-
@pytest.mark.asyncio(scope="session")
17-
async def test_fixture_loop_scopes(fixture):
18-
global loop
19-
assert loop == asyncio.get_running_loop()
28+
@pytest.mark.asyncio(scope="session")
29+
async def test_runs_in_same_loop_as_fixture(fixture):
30+
global loop
31+
assert loop == asyncio.get_running_loop()
32+
"""
33+
)
34+
)
35+
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
36+
result.assert_outcomes(passed=1)

0 commit comments

Comments
 (0)