Skip to content

Commit 30500e0

Browse files
committed
Document test execution and parametrization
1 parent 2c75c45 commit 30500e0

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

docs/concepts.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,15 @@ In *auto* mode pytest-asyncio automatically adds the *asyncio* marker to all asy
7171
This mode is intended for projects that use *asyncio* as their only asynchronous programming library. Auto mode makes for the simplest test and fixture configuration and is the recommended default.
7272

7373
If you intend to support multiple asynchronous programming libraries, e.g. *asyncio* and *trio*, strict mode will be the preferred option.
74+
75+
.. _concepts/concurrent_execution:
76+
77+
Test execution and concurrency
78+
==============================
79+
80+
pytest-asyncio runs async tests sequentially, just like how pytest runs synchronous tests. Each asynchronous test runs within its assigned event loop. For example, consider the following two tests:
81+
82+
.. include:: concepts_concurrent_execution_example.py
83+
:code: python
84+
85+
This sequential execution is intentional and important for maintaining test isolation. Running tests concurrently could introduce race conditions and side effects where one test could interfere with another, making test results unreliable and difficult to debug.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
6+
@pytest.mark.asyncio
7+
async def test_first():
8+
await asyncio.sleep(2) # Takes 2 seconds
9+
10+
11+
@pytest.mark.asyncio
12+
async def test_second():
13+
await asyncio.sleep(2) # Takes 2 seconds
14+
15+
16+
# Total execution time: ~4 seconds, not ~2 seconds

docs/how-to-guides/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ How-To Guides
1414
run_module_tests_in_same_loop
1515
run_package_tests_in_same_loop
1616
multiple_loops
17+
parametrize_with_asyncio
1718
uvloop
1819
test_item_is_async
1920

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=====================================
2+
How to parametrize asynchronous tests
3+
=====================================
4+
5+
The ``pytest.mark.parametrize`` marker works with asynchronous tests the same as with synchronous tests. You can apply both ``pytest.mark.asyncio`` and ``pytest.mark.parametrize`` to asynchronous test functions:
6+
7+
.. include:: parametrize_with_asyncio_example.py
8+
:code: python
9+
10+
.. note::
11+
Whilst asynchronous tests can be parametrized, each individual test case still runs sequentially, not concurrently. For more information about how pytest-asyncio executes tests, see :ref:`concepts/concurrent_execution`.
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+
6+
@pytest.mark.asyncio
7+
@pytest.mark.parametrize("value", [1, 2, 3])
8+
async def test_parametrized_async_function(value):
9+
await asyncio.sleep(1)
10+
assert value > 0

0 commit comments

Comments
 (0)