Skip to content

Fix example for all tests in session loop #792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ The easiest way to mark all tests is via a ``pytest_collection_modifyitems`` hoo

.. include:: session_scoped_loop_example.py
:code: python

Note that this will also override *all* manually applied marks in *strict* mode.
2 changes: 1 addition & 1 deletion docs/source/how-to-guides/session_scoped_loop_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ def pytest_collection_modifyitems(items):
pytest_asyncio_tests = (item for item in items if is_async_test(item))
session_scope_marker = pytest.mark.asyncio(scope="session")
for async_test in pytest_asyncio_tests:
async_test.add_marker(session_scope_marker)
async_test.add_marker(session_scope_marker, append=False)
63 changes: 63 additions & 0 deletions docs/source/how-to-guides/test_session_scoped_loop_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from pathlib import Path
from textwrap import dedent

from pytest import Pytester


def test_session_scoped_loop_configuration_works_in_auto_mode(
pytester: Pytester,
):
session_wide_mark_conftest = (
Path(__file__).parent / "session_scoped_loop_example.py"
)
pytester.makeconftest(session_wide_mark_conftest.read_text())
pytester.makepyfile(
dedent(
"""\
import asyncio

session_loop = None

async def test_store_loop(request):
global session_loop
session_loop = asyncio.get_running_loop()

async def test_compare_loop(request):
global session_loop
assert asyncio.get_running_loop() is session_loop
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=auto")
result.assert_outcomes(passed=2)


def test_session_scoped_loop_configuration_works_in_strict_mode(
pytester: Pytester,
):
session_wide_mark_conftest = (
Path(__file__).parent / "session_scoped_loop_example.py"
)
pytester.makeconftest(session_wide_mark_conftest.read_text())
pytester.makepyfile(
dedent(
"""\
import asyncio
import pytest

session_loop = None

@pytest.mark.asyncio
async def test_store_loop(request):
global session_loop
session_loop = asyncio.get_running_loop()

@pytest.mark.asyncio
async def test_compare_loop(request):
global session_loop
assert asyncio.get_running_loop() is session_loop
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(passed=2)