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
Copy file name to clipboardExpand all lines: doc/en/deprecations.rst
+72-70Lines changed: 72 additions & 70 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -148,76 +148,6 @@ Simply remove the ``__init__.py`` file entirely.
148
148
Python 3.3+ natively supports namespace packages without ``__init__.py``.
149
149
150
150
151
-
.. _sync-test-async-fixture:
152
-
153
-
sync test depending on async fixture
154
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
155
-
156
-
.. deprecated:: 8.4
157
-
158
-
Pytest has for a long time given an error when encountering an asynchronous test function, prompting the user to install
159
-
a plugin that can handle it. It has not given any errors if you have an asynchronous fixture that's depended on by a
160
-
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.
161
-
This is a problem even if you do have a plugin installed for handling async tests, as they may require
162
-
special decorators for async fixtures to be handled, and some may not robustly handle if a user accidentally requests an
163
-
async fixture from their sync tests. Fixture values being cached can make this even more unintuitive, where everything will
164
-
"work" if the fixture is first requested by an async test, and then requested by a synchronous test.
165
-
166
-
Unfortunately there is no 100% reliable method of identifying when a user has made a mistake, versus when they expect an
167
-
unawaited object from their fixture that they will handle on their own. To suppress this warning
168
-
when you in fact did intend to handle this you can wrap your async fixture in a synchronous fixture:
169
-
170
-
.. code-block:: python
171
-
172
-
import asyncio
173
-
import pytest
174
-
175
-
176
-
@pytest.fixture
177
-
asyncdefunawaited_fixture():
178
-
return1
179
-
180
-
181
-
deftest_foo(unawaited_fixture):
182
-
assert1== asyncio.run(unawaited_fixture)
183
-
184
-
should be changed to
185
-
186
-
187
-
.. code-block:: python
188
-
189
-
import asyncio
190
-
import pytest
191
-
192
-
193
-
@pytest.fixture
194
-
defunawaited_fixture():
195
-
asyncdefinner_fixture():
196
-
return1
197
-
198
-
return inner_fixture()
199
-
200
-
201
-
deftest_foo(unawaited_fixture):
202
-
assert1== asyncio.run(unawaited_fixture)
203
-
204
-
205
-
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.
206
-
207
-
If a user has an async fixture with ``autouse=True`` in their ``conftest.py``, or in a file
208
-
containing both synchronous tests and the fixture, they will receive this warning.
209
-
Unless you're using a plugin that specifically handles async fixtures
210
-
with synchronous tests, we strongly recommend against this practice.
211
-
It can lead to unpredictable behavior (with larger scopes, it may appear to "work" if an async
212
-
test is the first to request the fixture, due to value caching) and will generate
213
-
unawaited-coroutine runtime warnings (but only for non-yield fixtures).
214
-
Additionally, it creates ambiguity for other developers about whether the fixture is intended to perform
215
-
setup for synchronous tests.
216
-
217
-
The `anyio pytest plugin <https://anyio.readthedocs.io/en/stable/testing.html>`_ supports
218
-
synchronous tests with async fixtures, though certain limitations apply.
@@ -423,6 +353,78 @@ an appropriate period of deprecation has passed.
423
353
424
354
Some breaking changes which could not be deprecated are also listed.
425
355
356
+
.. _sync-test-async-fixture:
357
+
358
+
sync test depending on async fixture
359
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
360
+
361
+
.. deprecated:: 8.4
362
+
.. versionremoved:: 9.0
363
+
364
+
Pytest has for a long time given an error when encountering an asynchronous test function, prompting the user to install
365
+
a plugin that can handle it. It has not given any errors if you have an asynchronous fixture that's depended on by a
366
+
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.
367
+
This is a problem even if you do have a plugin installed for handling async tests, as they may require
368
+
special decorators for async fixtures to be handled, and some may not robustly handle if a user accidentally requests an
369
+
async fixture from their sync tests. Fixture values being cached can make this even more unintuitive, where everything will
370
+
"work" if the fixture is first requested by an async test, and then requested by a synchronous test.
371
+
372
+
Unfortunately there is no 100% reliable method of identifying when a user has made a mistake, versus when they expect an
373
+
unawaited object from their fixture that they will handle on their own. To suppress this warning
374
+
when you in fact did intend to handle this you can wrap your async fixture in a synchronous fixture:
375
+
376
+
.. code-block:: python
377
+
378
+
import asyncio
379
+
import pytest
380
+
381
+
382
+
@pytest.fixture
383
+
asyncdefunawaited_fixture():
384
+
return1
385
+
386
+
387
+
deftest_foo(unawaited_fixture):
388
+
assert1== asyncio.run(unawaited_fixture)
389
+
390
+
should be changed to
391
+
392
+
393
+
.. code-block:: python
394
+
395
+
import asyncio
396
+
import pytest
397
+
398
+
399
+
@pytest.fixture
400
+
defunawaited_fixture():
401
+
asyncdefinner_fixture():
402
+
return1
403
+
404
+
return inner_fixture()
405
+
406
+
407
+
deftest_foo(unawaited_fixture):
408
+
assert1== asyncio.run(unawaited_fixture)
409
+
410
+
411
+
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.
412
+
413
+
If a user has an async fixture with ``autouse=True`` in their ``conftest.py``, or in a file
414
+
containing both synchronous tests and the fixture, they will receive this warning.
415
+
Unless you're using a plugin that specifically handles async fixtures
416
+
with synchronous tests, we strongly recommend against this practice.
417
+
It can lead to unpredictable behavior (with larger scopes, it may appear to "work" if an async
418
+
test is the first to request the fixture, due to value caching) and will generate
419
+
unawaited-coroutine runtime warnings (but only for non-yield fixtures).
420
+
Additionally, it creates ambiguity for other developers about whether the fixture is intended to perform
421
+
setup for synchronous tests.
422
+
423
+
The `anyio pytest plugin <https://anyio.readthedocs.io/en/stable/testing.html>`_ supports
424
+
synchronous tests with async fixtures, though certain limitations apply.
0 commit comments