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
|
from pathlib import Path
from textwrap import dedent
from pytest import Pytester
def test_session_scoped_loop_configuration_works_in_auto_mode(
pytester: Pytester,
):
session_wide_mark_conftest = (
Path(__file__).parent / "session_scoped_loop_example.py"
)
pytester.makeconftest(session_wide_mark_conftest.read_text())
pytester.makepyfile(
dedent(
"""\
import asyncio
session_loop = None
async def test_store_loop(request):
global session_loop
session_loop = asyncio.get_running_loop()
async def test_compare_loop(request):
global session_loop
assert asyncio.get_running_loop() is session_loop
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=auto")
result.assert_outcomes(passed=2)
def test_session_scoped_loop_configuration_works_in_strict_mode(
pytester: Pytester,
):
session_wide_mark_conftest = (
Path(__file__).parent / "session_scoped_loop_example.py"
)
pytester.makeconftest(session_wide_mark_conftest.read_text())
pytester.makepyfile(
dedent(
"""\
import asyncio
import pytest
session_loop = None
@pytest.mark.asyncio
async def test_store_loop(request):
global session_loop
session_loop = asyncio.get_running_loop()
@pytest.mark.asyncio
async def test_compare_loop(request):
global session_loop
assert asyncio.get_running_loop() is session_loop
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(passed=2)
|