Skip to content

Commit a327200

Browse files
committed
[feat!] Replaced the asyncio_event_loop marker with an optional "scope" kwarg to the asyncio mark.
Signed-off-by: Michael Seifert <[email protected]>
1 parent 2cd541e commit a327200

17 files changed

+232
-263
lines changed

docs/source/reference/changelog.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22
Changelog
33
=========
44

5+
0.23.0 (UNRELEASED)
6+
===================
7+
This release is backwards-compatible with v0.21.
8+
Changes are non-breaking, unless you upgrade from v0.22.
9+
10+
BREAKING: The *asyncio_event_loop* mark has been removed. Class-scoped and module-scoped event loops can be requested
11+
via the *scope* keyword argument to the _asyncio_ mark.
12+
513
0.22.0 (2023-10-31)
614
===================
15+
This release has been yanked from PyPI due to fundamental issues with the _asyncio_event_loop_ mark.
16+
717
- Class-scoped and module-scoped event loops can be requested
818
via the _asyncio_event_loop_ mark. `#620 <https://github.com/pytest-dev/pytest-asyncio/pull/620>`_
919
- Deprecate redefinition of the `event_loop` fixture. `#587 <https://github.com/pytest-dev/pytest-asyncio/issues/531>`_
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
6+
class CustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
7+
pass
8+
9+
10+
@pytest.fixture(scope="module")
11+
def event_loop_policy(request):
12+
return CustomEventLoopPolicy()
13+
14+
15+
@pytest.mark.asyncio(scope="module")
16+
async def test_uses_custom_event_loop_policy():
17+
assert isinstance(asyncio.get_event_loop_policy(), CustomEventLoopPolicy)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import asyncio
2+
from asyncio import DefaultEventLoopPolicy
3+
4+
import pytest
5+
6+
7+
class CustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
8+
pass
9+
10+
11+
@pytest.fixture(
12+
params=(
13+
DefaultEventLoopPolicy(),
14+
CustomEventLoopPolicy(),
15+
),
16+
)
17+
def event_loop_policy(request):
18+
return request.param
19+
20+
21+
@pytest.mark.asyncio
22+
async def test_uses_custom_event_loop_policy():
23+
assert isinstance(asyncio.get_event_loop_policy(), DefaultEventLoopPolicy)

docs/source/reference/fixtures/index.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ If you need to change the type of the event loop, prefer setting a custom event
2222
If the ``pytest.mark.asyncio`` decorator is applied to a test function, the ``event_loop``
2323
fixture will be requested automatically by the test function.
2424

25+
event_loop_policy
26+
=================
27+
Returns the event loop policy used to create asyncio event loops.
28+
The default return value is *asyncio.get_event_loop_policy().*
29+
30+
This fixture can be overridden when a different event loop policy should be used.
31+
32+
.. include:: event_loop_policy_example.py
33+
:code: python
34+
35+
Multiple policies can be provided via fixture parameters.
36+
All tests to which the fixture is applied are run once for each fixture parameter.
37+
The following example runs the test with different event loop policies.
38+
39+
.. include:: event_loop_policy_parametrized_example.py
40+
:code: python
41+
2542
unused_tcp_port
2643
===============
2744
Finds and yields a single unused TCP port on the localhost interface. Useful for

docs/source/reference/markers/class_scoped_loop_auto_mode_example.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

docs/source/reference/markers/class_scoped_loop_custom_policy_strict_mode_example.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

docs/source/reference/markers/class_scoped_loop_strict_mode_example.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import pytest
44

55

6-
@pytest.mark.asyncio_event_loop
6+
@pytest.mark.asyncio(scope="class")
77
class TestClassScopedLoop:
88
loop: asyncio.AbstractEventLoop
99

10-
@pytest.mark.asyncio
1110
async def test_remember_loop(self):
1211
TestClassScopedLoop.loop = asyncio.get_running_loop()
1312

14-
@pytest.mark.asyncio
1513
async def test_this_runs_in_same_loop(self):
1614
assert asyncio.get_running_loop() is TestClassScopedLoop.loop

docs/source/reference/markers/class_scoped_loop_with_fixture_strict_mode_example.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
import pytest_asyncio
66

77

8-
@pytest.mark.asyncio_event_loop
8+
@pytest.mark.asyncio(scope="class")
99
class TestClassScopedLoop:
1010
loop: asyncio.AbstractEventLoop
1111

1212
@pytest_asyncio.fixture
1313
async def my_fixture(self):
1414
TestClassScopedLoop.loop = asyncio.get_running_loop()
1515

16-
@pytest.mark.asyncio
1716
async def test_runs_is_same_loop_as_fixture(self, my_fixture):
1817
assert asyncio.get_running_loop() is TestClassScopedLoop.loop
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
# Marks all test coroutines in this module
6+
pytestmark = pytest.mark.asyncio
7+
8+
9+
async def test_runs_in_asyncio_event_loop():
10+
assert asyncio.get_running_loop()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
6+
@pytest.mark.asyncio
7+
async def test_runs_in_asyncio_event_loop():
8+
assert asyncio.get_running_loop()

0 commit comments

Comments
 (0)