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 (
11+ dedent ("""\
12+ import asyncio
13+
14+ import pytest
15+
16+ class CustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
17+ pass
18+
19+ @pytest.fixture(
20+ params=[
21+ asyncio.DefaultEventLoopPolicy(),
22+ CustomEventLoopPolicy(),
23+ ],
24+ )
25+ def event_loop_policy(request):
26+ return request.param
27+
28+ @pytest.mark.asyncio
29+ async def test_async():
30+ assert isinstance(
31+ asyncio.get_event_loop_policy(),
32+ (asyncio.DefaultEventLoopPolicy, CustomEventLoopPolicy),
33+ )
34+
35+ def test_sync():
36+ assert True
37+ """ )
38+ )
39+
40+ result = pytester .runpytest ("--asyncio-mode=strict" )
41+
42+ result .assert_outcomes (passed = 3 )
You can’t perform that action at this time.
0 commit comments