Add a warning when setting up async fixtures #10839
Labels
topic: fixtures
anything involving fixtures directly or indirectly
type: enhancement
new feature or API change, should be merged into features branch
What's the problem this feature will solve?
Pytest cannot evaluate fixture that are async functions or async generators. Users of async pytest plugins may unintentionally annotate those functions with
@pytest.fixture
rather than the async plugin's fixture function.The fixture result ends up to be an unawaited async generator or coroutine, which is not what the user expected. Most of the time, the corresponding tests will fail. However, when the user defines an autouse fixture to perform setup or teardown, the fixture can silently fail.
Fixture results in async generator
Fixture results in coroutine
Fixture silently does nothing
Describe the solution you'd like
Pytest emits the following warning when running async test functions without an async plugin (see #2224):
The warning is emitted as part of a trylast hook to
pytest_pyfunc_call
. That means async pytest plugins can create hook wrappers to synchronize the test function and prevent the warning from being emitted.I suggest to add the same behavior to the use of
@pytest.fixture
on async functions or async generators.Examples
autouse=True
not executing properly in test pytest-asyncio#518Alternative Solutions
#9962 proposes a mechanism to await results of awaitable tests and fixtures. It can potentially address the issue described here.
Additional context
From the above list of async plugins, only anyio, pytest-asyncio, and pytest-trio make use of pytest_fixture_setup.
The text was updated successfully, but these errors were encountered: