Skip to content

Commit dac324c

Browse files
committed
WIP: Introduce event_loop_policy fixture.
Signed-off-by: Michael Seifert <[email protected]>
1 parent 549950f commit dac324c

File tree

5 files changed

+48
-24
lines changed

5 files changed

+48
-24
lines changed

docs/source/reference/markers/class_scoped_loop_custom_policies_strict_mode_example.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
import pytest
44

55

6-
@pytest.mark.asyncio_event_loop(
7-
policy=[
6+
@pytest.fixture(
7+
params=[
88
asyncio.DefaultEventLoopPolicy(),
99
asyncio.DefaultEventLoopPolicy(),
1010
]
1111
)
12+
def event_loop_policy(request):
13+
return request.param
14+
15+
1216
class TestWithDifferentLoopPolicies:
1317
@pytest.mark.asyncio
1418
async def test_parametrized_loop(self):

docs/source/reference/markers/class_scoped_loop_custom_policy_strict_mode_example.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ class CustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
77
pass
88

99

10-
@pytest.mark.asyncio_event_loop(policy=CustomEventLoopPolicy())
10+
@pytest.fixture(scope="class")
11+
def event_loop_policy(request):
12+
return CustomEventLoopPolicy()
13+
14+
15+
@pytest.mark.asyncio_event_loop
1116
class TestUsesCustomEventLoopPolicy:
1217
@pytest.mark.asyncio
1318
async def test_uses_custom_event_loop_policy(self):

pytest_asyncio/plugin.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import inspect
77
import socket
88
import warnings
9+
from asyncio import AbstractEventLoopPolicy
910
from textwrap import dedent
1011
from typing import (
1112
Any,
@@ -553,12 +554,6 @@ def pytest_collectstart(collector: pytest.Collector):
553554
for mark in marks:
554555
if not mark.name == "asyncio_event_loop":
555556
continue
556-
event_loop_policy = mark.kwargs.get("policy", asyncio.get_event_loop_policy())
557-
policy_params = (
558-
event_loop_policy
559-
if isinstance(event_loop_policy, Iterable)
560-
else (event_loop_policy,)
561-
)
562557

563558
# There seem to be issues when a fixture is shadowed by another fixture
564559
# and both differ in their params.
@@ -573,14 +568,12 @@ def pytest_collectstart(collector: pytest.Collector):
573568
@pytest.fixture(
574569
scope="class" if isinstance(collector, pytest.Class) else "module",
575570
name=event_loop_fixture_id,
576-
params=policy_params,
577-
ids=tuple(type(policy).__name__ for policy in policy_params),
578571
)
579572
def scoped_event_loop(
580573
*args, # Function needs to accept "cls" when collected by pytest.Class
581-
request,
574+
event_loop_policy,
582575
) -> Iterator[asyncio.AbstractEventLoop]:
583-
new_loop_policy = request.param
576+
new_loop_policy = event_loop_policy
584577
old_loop_policy = asyncio.get_event_loop_policy()
585578
old_loop = asyncio.get_event_loop()
586579
asyncio.set_event_loop_policy(new_loop_policy)
@@ -867,6 +860,12 @@ def event_loop(request: FixtureRequest) -> Iterator[asyncio.AbstractEventLoop]:
867860
loop.close()
868861

869862

863+
@pytest.fixture(scope="session", autouse=True)
864+
def event_loop_policy() -> AbstractEventLoopPolicy:
865+
"""Return an instance of the policy used to create asyncio event loop."""
866+
return asyncio.get_event_loop_policy()
867+
868+
870869
def _unused_port(socket_type: int) -> int:
871870
"""Find an unused localhost port from 1024-65535 and return it."""
872871
with contextlib.closing(socket.socket(type=socket_type)) as sock:

tests/markers/test_class_marker.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,12 @@ def test_asyncio_event_loop_mark_allows_specifying_the_loop_policy(
140140
class CustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
141141
pass
142142
143-
@pytest.mark.asyncio_event_loop(policy=CustomEventLoopPolicy())
144-
class TestUsesCustomEventLoopPolicy:
143+
@pytest.fixture(scope="class")
144+
def event_loop_policy():
145+
return CustomEventLoopPolicy()
146+
147+
@pytest.mark.asyncio_event_loop
148+
class TestUsesCustomEventLoop:
145149
146150
@pytest.mark.asyncio
147151
async def test_uses_custom_event_loop_policy(self):
@@ -173,15 +177,18 @@ def test_asyncio_event_loop_mark_allows_specifying_multiple_loop_policies(
173177
174178
import pytest
175179
176-
@pytest.mark.asyncio_event_loop(
177-
policy=[
180+
@pytest.fixture(
181+
params=[
178182
asyncio.DefaultEventLoopPolicy(),
179183
asyncio.DefaultEventLoopPolicy(),
180184
]
181185
)
186+
def event_loop_policy(request):
187+
return request.param
188+
182189
class TestWithDifferentLoopPolicies:
183190
@pytest.mark.asyncio
184-
async def test_parametrized_loop(self):
191+
async def test_parametrized_loop(self, request):
185192
pass
186193
"""
187194
)

tests/markers/test_module_marker.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,11 @@ class CustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
157157
158158
from .custom_policy import CustomEventLoopPolicy
159159
160-
pytestmark = pytest.mark.asyncio_event_loop(policy=CustomEventLoopPolicy())
160+
pytestmark = pytest.mark.asyncio_event_loop
161+
162+
@pytest.fixture(scope="module")
163+
def event_loop_policy():
164+
return CustomEventLoopPolicy()
161165
162166
@pytest.mark.asyncio
163167
async def test_uses_custom_event_loop_policy():
@@ -178,7 +182,7 @@ async def test_uses_custom_event_loop_policy():
178182
async def test_does_not_use_custom_event_loop_policy():
179183
assert not isinstance(
180184
asyncio.get_event_loop_policy(),
181-
CustomEventLoopPolicy,
185+
CustomEventLoopPolicy,
182186
)
183187
"""
184188
),
@@ -197,20 +201,25 @@ def test_asyncio_event_loop_mark_allows_specifying_multiple_loop_policies(
197201
198202
import pytest
199203
200-
pytestmark = pytest.mark.asyncio_event_loop(
201-
policy=[
204+
pytestmark = pytest.mark.asyncio_event_loop
205+
206+
@pytest.fixture(
207+
scope="module",
208+
params=[
202209
asyncio.DefaultEventLoopPolicy(),
203210
asyncio.DefaultEventLoopPolicy(),
204-
]
211+
],
205212
)
213+
def event_loop_policy(request):
214+
return request.param
206215
207216
@pytest.mark.asyncio
208217
async def test_parametrized_loop():
209218
pass
210219
"""
211220
)
212221
)
213-
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
222+
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "--setup-show")
214223
result.assert_outcomes(passed=2)
215224

216225

0 commit comments

Comments
 (0)