|
3 | 3 | ==========
|
4 | 4 | Decorators
|
5 | 5 | ==========
|
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. |
7 | 7 |
|
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. |
10 | 16 |
|
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 |
15 | 21 |
|
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. |
0 commit comments