Skip to content

Commit b22d84e

Browse files
committed
[docs] Fixes the example showing how to run all tests in a session-scoped loop.
The change also adds a warning that the code snippet overrides all manually applied marks in strict mode and adds tests verifying the correctness of the example. Signed-off-by: Michael Seifert <[email protected]>
1 parent f1de446 commit b22d84e

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

docs/source/how-to-guides/run_session_tests_in_same_loop.rst

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ The easiest way to mark all tests is via a ``pytest_collection_modifyitems`` hoo
66

77
.. include:: session_scoped_loop_example.py
88
:code: python
9+
10+
Note that this will also override *all* manually applied marks in *strict* mode.

docs/source/how-to-guides/session_scoped_loop_example.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ def pytest_collection_modifyitems(items):
77
pytest_asyncio_tests = (item for item in items if is_async_test(item))
88
session_scope_marker = pytest.mark.asyncio(scope="session")
99
for async_test in pytest_asyncio_tests:
10-
async_test.add_marker(session_scope_marker)
10+
async_test.add_marker(session_scope_marker, append=False)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from pathlib import Path
2+
from textwrap import dedent
3+
4+
from pytest import Pytester
5+
6+
7+
def test_session_scoped_loop_configuration_works_in_auto_mode(
8+
pytester: Pytester,
9+
):
10+
session_wide_mark_conftest = (
11+
Path(__file__).parent / "session_scoped_loop_example.py"
12+
)
13+
pytester.makeconftest(session_wide_mark_conftest.read_text())
14+
pytester.makepyfile(
15+
dedent(
16+
"""\
17+
import asyncio
18+
19+
session_loop = None
20+
21+
async def test_store_loop(request):
22+
global session_loop
23+
session_loop = asyncio.get_running_loop()
24+
25+
async def test_compare_loop(request):
26+
global session_loop
27+
assert asyncio.get_running_loop() is session_loop
28+
"""
29+
)
30+
)
31+
result = pytester.runpytest_subprocess("--asyncio-mode=auto")
32+
result.assert_outcomes(passed=2)
33+
34+
35+
def test_session_scoped_loop_configuration_works_in_strict_mode(
36+
pytester: Pytester,
37+
):
38+
session_wide_mark_conftest = (
39+
Path(__file__).parent / "session_scoped_loop_example.py"
40+
)
41+
pytester.makeconftest(session_wide_mark_conftest.read_text())
42+
pytester.makepyfile(
43+
dedent(
44+
"""\
45+
import asyncio
46+
import pytest
47+
48+
session_loop = None
49+
50+
@pytest.mark.asyncio
51+
async def test_store_loop(request):
52+
global session_loop
53+
session_loop = asyncio.get_running_loop()
54+
55+
@pytest.mark.asyncio
56+
async def test_compare_loop(request):
57+
global session_loop
58+
assert asyncio.get_running_loop() is session_loop
59+
"""
60+
)
61+
)
62+
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
63+
result.assert_outcomes(passed=2)

0 commit comments

Comments
 (0)