You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Synchronous tests that request an asynchronous fixture will now give a DeprecationWarning. This will introduce warnings in several pytest plugins that handle async tests/fixtures and for some users with custom setups. For guidance on how to manage this see :ref:`sync-test-async-fixture`.
1
+
Requesting an asynchronous fixture without a `pytest_fixture_setup` to handle it will now give a DeprecationWarning. This most commonly happens if a sync test requests an async fixture. This should have no effect on a majority of users with async tests or fixtures, but may affect non-standard hook setups or ``autouse=True``. For guidance on how to work around this warning see :ref:`sync-test-async-fixture`.
Copy file name to clipboardExpand all lines: doc/en/deprecations.rst
+7-3Lines changed: 7 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,13 +24,14 @@ sync test depending on async fixture
24
24
25
25
Pytest has for a long time given an error when encountering an asynchronous test function, prompting the user to install
26
26
a plugin that can handle it. It has not given any errors if you have an asynchronous fixture that's depended on by a
27
-
synchronous test. This is a problem even if you do have a plugin installed for handling async tests, as they may require
27
+
synchronous test. If the fixture was an async function you did get an "unawaited coroutine" warning, but for async yield fixtures you didn't even get that.
28
+
This is a problem even if you do have a plugin installed for handling async tests, as they may require
28
29
special decorators for async fixtures to be handled, and some may not robustly handle if a user accidentally requests an
29
30
async fixture from their sync tests. Fixture values being cached can make this even more unintuitive, where everything will
30
31
"work" if the fixture is first requested by an async test, and then requested by a synchronous test.
31
32
32
33
Unfortunately there is no 100% reliable method of identifying when a user has made a mistake, versus when they expect an
33
-
unawaited object from their fixture that they will handle - either on their own, or by a plugin. To suppress this warning
34
+
unawaited object from their fixture that they will handle on their own. To suppress this warning
34
35
when you in fact did intend to handle this you can wrap your async fixture in a synchronous fixture:
35
36
36
37
.. code-block:: python
@@ -67,7 +68,10 @@ should be changed to
67
68
deftest_foo(unawaited_fixture):
68
69
assert1== asyncio.run(unawaited_fixture)
69
70
70
-
If a user has an async fixture with ``autouse=True`` in their ``conftest.py``, or in a file where they also have synchronous tests, they will also get this warning. We strongly recommend against this practice, and they should restructure their testing infrastructure so the fixture is synchronous or to separate the fixture from their synchronous tests. Plugins that handle async may want to introduce helpers to make that easier in scenarios where that might be wanted, e.g. if setting up a database in an asynchronous way, or the user may opt to make their test async even though it might not strictly need to be.
71
+
72
+
You can also make use of `pytest_fixture_setup` to handle the coroutine/asyncgen before pytest sees it - this is the way current async pytest plugins handle it.
73
+
74
+
If a user has an async fixture with ``autouse=True`` in their ``conftest.py``, or in a file where they also have synchronous tests, they will also get this warning. We strongly recommend against this practice, and they should restructure their testing infrastructure so the fixture is synchronous or to separate the fixture from their synchronous tests. Note that the anyio pytest plugin has some support for sync test + async fixtures currently.
0 commit comments