Decorators
The @pytest_asyncio.fixture
decorator allows coroutines and async generator functions to be used as pytest fixtures.
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 asyncio 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.
Examples:
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(): ...
auto mode automatically converts coroutines and async generator functions declared with the standard @pytest.fixture
decorator to pytest-asyncio fixtures.