-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test] Test the "loop_scope=session" argument to pytest_asyncio.fixtu…
…re with additional pytest fixture scopes. Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de>
- Loading branch information
Showing
1 changed file
with
29 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |