@@ -352,6 +352,47 @@ def pytest_asyncio_loop_factories(config, item):
352352 return {"sub": SubCustomEventLoop}
353353 """ ),
354354 )
355+ subdir .joinpath ("test_sub.py" ).write_text (
356+ dedent ("""\
357+ import asyncio
358+ import pytest
359+
360+ pytest_plugins = "pytest_asyncio"
361+
362+ @pytest.mark.asyncio
363+ async def test_uses_sub_loop():
364+ assert type(asyncio.get_running_loop()).__name__ == "SubCustomEventLoop"
365+ """ ),
366+ )
367+ result = pytester .runpytest ("--asyncio-mode=strict" )
368+ result .assert_outcomes (passed = 1 )
369+
370+
371+ def test_nested_conftest_hook_respects_conftest_locality (
372+ pytester : Pytester ,
373+ ) -> None :
374+ pytester .makeini ("[pytest]\n asyncio_default_fixture_loop_scope = function" )
375+ pytester .makeconftest (dedent ("""\
376+ import asyncio
377+
378+ class RootCustomEventLoop(asyncio.SelectorEventLoop):
379+ pass
380+
381+ def pytest_asyncio_loop_factories(config, item):
382+ return {"root": RootCustomEventLoop}
383+ """ ))
384+ subdir = pytester .mkdir ("subdir" )
385+ subdir .joinpath ("conftest.py" ).write_text (
386+ dedent ("""\
387+ import asyncio
388+
389+ class SubCustomEventLoop(asyncio.SelectorEventLoop):
390+ pass
391+
392+ def pytest_asyncio_loop_factories(config, item):
393+ return {"sub": SubCustomEventLoop}
394+ """ ),
395+ )
355396 pytester .makepyfile (
356397 test_root = dedent ("""\
357398 import asyncio
@@ -360,8 +401,10 @@ def pytest_asyncio_loop_factories(config, item):
360401 pytest_plugins = "pytest_asyncio"
361402
362403 @pytest.mark.asyncio
363- async def test_uses_sub_loop():
364- assert type(asyncio.get_running_loop()).__name__ == "SubCustomEventLoop"
404+ async def test_root_uses_root_loop():
405+ assert (
406+ type(asyncio.get_running_loop()).__name__ == "RootCustomEventLoop"
407+ )
365408 """ ),
366409 )
367410 subdir .joinpath ("test_sub.py" ).write_text (
@@ -372,7 +415,7 @@ async def test_uses_sub_loop():
372415 pytest_plugins = "pytest_asyncio"
373416
374417 @pytest.mark.asyncio
375- async def test_uses_sub_loop ():
418+ async def test_sub_uses_sub_loop ():
376419 assert type(asyncio.get_running_loop()).__name__ == "SubCustomEventLoop"
377420 """ ),
378421 )
0 commit comments