File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ Parametrized ``event_loop_policy `` fixtures no longer parametrize synchronous tests.
Original file line number Diff line number Diff line change @@ -730,6 +730,7 @@ def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
730730 metafunc .definition
731731 )
732732 if specialized_item_class is None :
733+ _remove_autouse_event_loop_policy_for_sync_tests (metafunc )
733734 return
734735
735736 asyncio_marker = _resolve_asyncio_marker (metafunc .definition )
@@ -788,6 +789,25 @@ def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
788789 )
789790
790791
792+ def _remove_autouse_event_loop_policy_for_sync_tests (
793+ metafunc : pytest .Metafunc ,
794+ ) -> None :
795+ fixture_name = event_loop_policy .__name__
796+ fixtureinfo = metafunc .definition ._fixtureinfo
797+ if fixture_name in fixtureinfo .argnames :
798+ return
799+ if any (
800+ fixture_name in marker .args
801+ for marker in metafunc .definition .iter_markers (name = "usefixtures" )
802+ ):
803+ return
804+ if fixture_name in metafunc .fixturenames :
805+ metafunc .fixturenames .remove (fixture_name )
806+ if fixture_name in fixtureinfo .names_closure :
807+ fixtureinfo .names_closure .remove (fixture_name )
808+ fixtureinfo .name2fixturedefs .pop (fixture_name , None )
809+
810+
791811@contextlib .contextmanager
792812def _temporary_event_loop (loop : AbstractEventLoop ) -> Iterator [None ]:
793813 try :
Original file line number Diff line number Diff line change 1+ from textwrap import dedent
2+
3+ from pytest import Pytester
4+
5+
6+ def test_parametrized_loop_policy_does_not_parametrize_sync_tests (
7+ pytester : Pytester ,
8+ ):
9+ pytester .makeini ("[pytest]\n asyncio_default_fixture_loop_scope = function" )
10+ pytester .makepyfile (dedent ("""\
11+ import asyncio
12+
13+ import pytest
14+
15+ class CustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
16+ pass
17+
18+ @pytest.fixture(
19+ params=[
20+ asyncio.DefaultEventLoopPolicy(),
21+ CustomEventLoopPolicy(),
22+ ],
23+ )
24+ def event_loop_policy(request):
25+ return request.param
26+
27+ @pytest.mark.asyncio
28+ async def test_async():
29+ assert isinstance(
30+ asyncio.get_event_loop_policy(),
31+ (asyncio.DefaultEventLoopPolicy, CustomEventLoopPolicy),
32+ )
33+
34+ def test_sync():
35+ assert True
36+ """ ))
37+
38+ result = pytester .runpytest ("--asyncio-mode=strict" )
39+
40+ result .assert_outcomes (passed = 3 )
You can’t perform that action at this time.
0 commit comments