Skip to content

Commit

Permalink
docs: Updated documentation on decorators to address the addition of …
Browse files Browse the repository at this point in the history
…the loop_scope keyword argument.

Signed-off-by: Michael Seifert <[email protected]>
  • Loading branch information
seifertm committed Jul 25, 2024
1 parent a89b0fe commit a4d64c8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
2 changes: 2 additions & 0 deletions docs/source/concepts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Concepts
========

.. _concepts/event_loops:

asyncio event loops
===================
In order to understand how pytest-asyncio works, it helps to understand how pytest collectors work.
Expand Down
14 changes: 0 additions & 14 deletions docs/source/reference/decorators/fixture_strict_mode_example.py

This file was deleted.

23 changes: 14 additions & 9 deletions docs/source/reference/decorators/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
==========
Decorators
==========
Asynchronous fixtures are defined just like ordinary pytest fixtures, except they should be decorated with ``@pytest_asyncio.fixture``.
The ``@pytest_asyncio.fixture`` decorator allows coroutines and async generator functions to be used as pytest fixtures.

.. include:: fixture_strict_mode_example.py
:code: python
The decorator takes all arguments supported by `@pytest.fixture`.
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`).
The default event loop scope is *function* scope.
Possible loop scopes are *session,* *package,* *module,* *class,* and *function*.

The *loop_scope* of a fixture can be chosen independently from its caching *scope*.
However, the event loop scope must be larger or the same as the fixture's caching scope.
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.

All scopes are supported, but if you use a non-function scope you will need
to redefine the ``event_loop`` fixture to have the same or broader scope.
Async fixtures need the event loop, and so must have the same or narrower scope
than the ``event_loop`` fixture.
Examples:

.. include:: pytest_asyncio_fixture_example.py
:code: python

*auto* mode automatically converts async fixtures declared with the
standard ``@pytest.fixture`` decorator to *asyncio-driven* versions.
*auto* mode automatically converts coroutines and async generator functions declared with the standard ``@pytest.fixture`` decorator to pytest-asyncio fixtures.
17 changes: 17 additions & 0 deletions docs/source/reference/decorators/pytest_asyncio_fixture_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest_asyncio


@pytest_asyncio.fixture
async def fixture_runs_in_fresh_loop_for_every_function(): ...


@pytest_asyncio.fixture(loop_scope="session", scope="module")
async def fixture_runs_in_session_loop_once_per_module(): ...


@pytest_asyncio.fixture(loop_scope="module", scope="module")
async def fixture_runs_in_module_loop_once_per_module(): ...


@pytest_asyncio.fixture(loop_scope="module")
async def fixture_runs_in_module_loop_once_per_function(): ...

0 comments on commit a4d64c8

Please sign in to comment.