Skip to content

Commit a4d64c8

Browse files
committed
docs: Updated documentation on decorators to address the addition of the loop_scope keyword argument.
Signed-off-by: Michael Seifert <[email protected]>
1 parent a89b0fe commit a4d64c8

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

docs/source/concepts.rst

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Concepts
33
========
44

5+
.. _concepts/event_loops:
6+
57
asyncio event loops
68
===================
79
In order to understand how pytest-asyncio works, it helps to understand how pytest collectors work.

docs/source/reference/decorators/fixture_strict_mode_example.py

-14
This file was deleted.

docs/source/reference/decorators/index.rst

+14-9
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33
==========
44
Decorators
55
==========
6-
Asynchronous fixtures are defined just like ordinary pytest fixtures, except they should be decorated with ``@pytest_asyncio.fixture``.
6+
The ``@pytest_asyncio.fixture`` decorator allows coroutines and async generator functions to be used as pytest fixtures.
77

8-
.. include:: fixture_strict_mode_example.py
9-
:code: python
8+
The decorator takes all arguments supported by `@pytest.fixture`.
9+
Additionally, ``@pytest_asyncio.fixture`` supports the *loop_scope* keyword argument, which selects the event loop in which the fixture is run (see :ref:`concepts/event_loops`).
10+
The default event loop scope is *function* scope.
11+
Possible loop scopes are *session,* *package,* *module,* *class,* and *function*.
12+
13+
The *loop_scope* of a fixture can be chosen independently from its caching *scope*.
14+
However, the event loop scope must be larger or the same as the fixture's caching scope.
15+
In other words, it's possible to reevaluate an async fixture multiple times within the same event loop, but it's not possible to switch out the running event loop in an async fixture.
1016

11-
All scopes are supported, but if you use a non-function scope you will need
12-
to redefine the ``event_loop`` fixture to have the same or broader scope.
13-
Async fixtures need the event loop, and so must have the same or narrower scope
14-
than the ``event_loop`` fixture.
17+
Examples:
18+
19+
.. include:: pytest_asyncio_fixture_example.py
20+
:code: python
1521

16-
*auto* mode automatically converts async fixtures declared with the
17-
standard ``@pytest.fixture`` decorator to *asyncio-driven* versions.
22+
*auto* mode automatically converts coroutines and async generator functions declared with the standard ``@pytest.fixture`` decorator to pytest-asyncio fixtures.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest_asyncio
2+
3+
4+
@pytest_asyncio.fixture
5+
async def fixture_runs_in_fresh_loop_for_every_function(): ...
6+
7+
8+
@pytest_asyncio.fixture(loop_scope="session", scope="module")
9+
async def fixture_runs_in_session_loop_once_per_module(): ...
10+
11+
12+
@pytest_asyncio.fixture(loop_scope="module", scope="module")
13+
async def fixture_runs_in_module_loop_once_per_module(): ...
14+
15+
16+
@pytest_asyncio.fixture(loop_scope="module")
17+
async def fixture_runs_in_module_loop_once_per_function(): ...

0 commit comments

Comments
 (0)