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
Requesting an asynchronous fixture without a `pytest_fixture_setup` hook that resolves 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 using async pytest plugins, 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`.
Test functions containing a yield now cause an explicit error. They have not been run since pytest 4.0, and were previously marked as an expected failure and deprecation warning.
2
+
3
+
See :ref:`the docs <yield tests deprecated>` for more information.
Clarify :ref:`filterwarnings` docs on filter precedence/order when using multiple :ref:`@pytest.mark.filterwarnings <pytest.mark.filterwarnings ref>` marks.
@@ -15,6 +15,76 @@ Below is a complete list of all pytest features which are considered deprecated.
15
15
:class:`~pytest.PytestWarning` or subclasses, which can be filtered using :ref:`standard warning filters <warnings>`.
16
16
17
17
18
+
.. _sync-test-async-fixture:
19
+
20
+
sync test depending on async fixture
21
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22
+
23
+
.. deprecated:: 8.4
24
+
25
+
Pytest has for a long time given an error when encountering an asynchronous test function, prompting the user to install
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. 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
29
+
special decorators for async fixtures to be handled, and some may not robustly handle if a user accidentally requests an
30
+
async fixture from their sync tests. Fixture values being cached can make this even more unintuitive, where everything will
31
+
"work" if the fixture is first requested by an async test, and then requested by a synchronous test.
32
+
33
+
Unfortunately there is no 100% reliable method of identifying when a user has made a mistake, versus when they expect an
34
+
unawaited object from their fixture that they will handle on their own. To suppress this warning
35
+
when you in fact did intend to handle this you can wrap your async fixture in a synchronous fixture:
36
+
37
+
.. code-block:: python
38
+
39
+
import asyncio
40
+
import pytest
41
+
42
+
43
+
@pytest.fixture
44
+
asyncdefunawaited_fixture():
45
+
return1
46
+
47
+
48
+
deftest_foo(unawaited_fixture):
49
+
assert1== asyncio.run(unawaited_fixture)
50
+
51
+
should be changed to
52
+
53
+
54
+
.. code-block:: python
55
+
56
+
import asyncio
57
+
import pytest
58
+
59
+
60
+
@pytest.fixture
61
+
defunawaited_fixture():
62
+
asyncdefinner_fixture():
63
+
return1
64
+
65
+
return inner_fixture()
66
+
67
+
68
+
deftest_foo(unawaited_fixture):
69
+
assert1== asyncio.run(unawaited_fixture)
70
+
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
75
+
containing both synchronous tests and the fixture, they will receive this warning.
76
+
Unless you're using a plugin that specifically handles async fixtures
77
+
with synchronous tests, we strongly recommend against this practice.
78
+
It can lead to unpredictable behavior (with larger scopes, it may appear to "work" if an async
79
+
test is the first to request the fixture, due to value caching) and will generate
80
+
unawaited-coroutine runtime warnings (but only for non-yield fixtures).
81
+
Additionally, it creates ambiguity for other developers about whether the fixture is intended to perform
82
+
setup for synchronous tests.
83
+
84
+
The `anyio pytest plugin <https://anyio.readthedocs.io/en/stable/testing.html>`_ supports
85
+
synchronous tests with async fixtures, though certain limitations apply.
0 commit comments