Skip to content

Commit cacb156

Browse files
committed
[docs] Added how-to guides for testing with different loop scopes.
Signed-off-by: Michael Seifert <[email protected]>
1 parent febdb8d commit cacb156

9 files changed

+83
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
6+
@pytest.mark.asyncio(scope="class")
7+
class TestInOneEventLoopPerClass:
8+
loop: asyncio.AbstractEventLoop
9+
10+
async def test_remember_loop(self):
11+
TestInOneEventLoopPerClass.loop = asyncio.get_running_loop()
12+
13+
async def test_assert_same_loop(self):
14+
assert asyncio.get_running_loop() is TestInOneEventLoopPerClass.loop

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

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ How-To Guides
55
.. toctree::
66
:hidden:
77

8+
run_class_tests_in_same_loop
9+
run_module_tests_in_same_loop
10+
run_package_tests_in_same_loop
11+
run_session_tests_in_same_loop
812
multiple_loops
913
uvloop
1014
test_item_is_async
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
pytestmark = pytest.mark.asyncio(scope="module")
6+
7+
loop: asyncio.AbstractEventLoop
8+
9+
10+
async def test_remember_loop():
11+
global loop
12+
loop = asyncio.get_running_loop()
13+
14+
15+
async def test_assert_same_loop():
16+
global loop
17+
assert asyncio.get_running_loop() is loop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import pytest
2+
3+
pytestmark = pytest.mark.asyncio(scope="package")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
======================================================
2+
How to run all tests in a class in the same event loop
3+
======================================================
4+
All tests can be run inside the same event loop by marking them with ``pytest.mark.asyncio(scope="class")``.
5+
This is easily achieved by using the *asyncio* marker as a class decorator.
6+
7+
.. include:: class_scoped_loop_example.py
8+
:code: python
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=======================================================
2+
How to run all tests in a module in the same event loop
3+
=======================================================
4+
All tests can be run inside the same event loop by marking them with ``pytest.mark.asyncio(scope="module")``.
5+
This is easily achieved by adding a `pytestmark` statement to your module.
6+
7+
.. include:: module_scoped_loop_example.py
8+
:code: python
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
========================================================
2+
How to run all tests in a package in the same event loop
3+
========================================================
4+
All tests can be run inside the same event loop by marking them with ``pytest.mark.asyncio(scope="package")``.
5+
Add the following code to the ``__init__.py`` of the test package:
6+
7+
.. include:: package_scoped_loop_example.py
8+
:code: python
9+
10+
Note that this marker is not passed down to tests in subpackages.
11+
Subpackages constitute their own, separate package.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
==========================================================
2+
How to run all tests in the session in the same event loop
3+
==========================================================
4+
All tests can be run inside the same event loop by marking them with ``pytest.mark.asyncio(scope="session")``.
5+
The easiest way to mark all tests is via a ``pytest_collection_modifyitems`` hook in the ``conftest.py`` at the root folder of your test suite.
6+
7+
.. include:: session_scoped_loop_example.py
8+
:code: python
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pytest
2+
3+
from pytest_asyncio import is_async_test
4+
5+
6+
def pytest_collection_modifyitems(items):
7+
pytest_asyncio_tests = (item for item in items if is_async_test(item))
8+
session_scope_marker = pytest.mark.asyncio(scope="session")
9+
for async_test in pytest_asyncio_tests:
10+
async_test.add_marker(session_scope_marker)

0 commit comments

Comments
 (0)