Skip to content

Commit

Permalink
[test] Test the "loop_scope=session" argument to pytest_asyncio.fixtu…
Browse files Browse the repository at this point in the history
…re with additional pytest fixture scopes.

Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de>
  • Loading branch information
seifertm committed Jul 5, 2024
1 parent 412a73e commit 8daa107
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions tests/test_fixture_loop_scopes.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
import asyncio
from textwrap import dedent

import pytest
from pytest import Pytester

import pytest_asyncio

loop: asyncio.AbstractEventLoop
@pytest.mark.parametrize(
"fixture_scope", ("session", "package", "module", "class", "function")
)
def test_loop_scope_session_is_independent_of_fixture_scope(
pytester: Pytester,
fixture_scope: str,
):
pytester.makepyfile(
dedent(
f"""\
import asyncio
import pytest
import pytest_asyncio
loop: asyncio.AbstractEventLoop = None
@pytest_asyncio.fixture(loop_scope="session")
async def fixture():
global loop
loop = asyncio.get_running_loop()
@pytest_asyncio.fixture(scope="{fixture_scope}", loop_scope="session")
async def fixture():
global loop
loop = asyncio.get_running_loop()

@pytest.mark.asyncio(scope="session")
async def test_fixture_loop_scopes(fixture):
global loop
assert loop == asyncio.get_running_loop()
@pytest.mark.asyncio(scope="session")
async def test_runs_in_same_loop_as_fixture(fixture):
global loop
assert loop == asyncio.get_running_loop()
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(passed=1)

0 comments on commit 8daa107

Please sign in to comment.