1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
from __future__ import annotations
from textwrap import dedent
import pytest
from pytest import Pytester
@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(scope="{fixture_scope}", loop_scope="session")
async def fixture():
global loop
loop = asyncio.get_running_loop()
@pytest.mark.asyncio(loop_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)
@pytest.mark.parametrize("default_loop_scope", ("function", "module", "session"))
def test_default_loop_scope_config_option_changes_fixture_loop_scope(
pytester: Pytester,
default_loop_scope: str,
):
pytester.makeini(
dedent(
f"""\
[pytest]
asyncio_default_fixture_loop_scope = {default_loop_scope}
"""
)
)
pytester.makepyfile(
dedent(
f"""\
import asyncio
import pytest
import pytest_asyncio
@pytest_asyncio.fixture
async def fixture_loop():
return asyncio.get_running_loop()
@pytest.mark.asyncio(loop_scope="{default_loop_scope}")
async def test_runs_in_fixture_loop(fixture_loop):
assert asyncio.get_running_loop() is fixture_loop
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(passed=1)
def test_default_class_loop_scope_config_option_changes_fixture_loop_scope(
pytester: Pytester,
):
pytester.makeini(
dedent(
"""\
[pytest]
asyncio_default_fixture_loop_scope = class
"""
)
)
pytester.makepyfile(
dedent(
"""\
import asyncio
import pytest
import pytest_asyncio
class TestClass:
@pytest_asyncio.fixture
async def fixture_loop(self):
return asyncio.get_running_loop()
@pytest.mark.asyncio(loop_scope="class")
async def test_runs_in_fixture_loop(self, fixture_loop):
assert asyncio.get_running_loop() is fixture_loop
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(passed=1)
def test_default_package_loop_scope_config_option_changes_fixture_loop_scope(
pytester: Pytester,
):
pytester.makeini(
dedent(
"""\
[pytest]
asyncio_default_fixture_loop_scope = package
"""
)
)
pytester.makepyfile(
__init__="",
test_a=dedent(
"""\
import asyncio
import pytest
import pytest_asyncio
@pytest_asyncio.fixture
async def fixture_loop():
return asyncio.get_running_loop()
@pytest.mark.asyncio(loop_scope="package")
async def test_runs_in_fixture_loop(fixture_loop):
assert asyncio.get_running_loop() is fixture_loop
"""
),
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(passed=1)
|