-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Fixes the example showing how to run all tests in a session-sc…
…oped 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]>
- Loading branch information
Showing
3 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
docs/source/how-to-guides/test_session_scoped_loop_example.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |