Skip to content

Commit 8a86c9e

Browse files
committed
fix: Fixed a regression that prevented async fixtures from working in sync tests.
The commit simply processes all async fixtures of a test regardless of whether the test is an async function or not. Closes pytest-dev#286 Signed-off-by: Michael Seifert <[email protected]>
1 parent 07e9922 commit 8a86c9e

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

README.rst

+4
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ or an async framework such as `asynctest <https://asynctest.readthedocs.io/en/la
257257
Changelog
258258
---------
259259

260+
0.18.1 (UNRELEASED)
261+
~~~~~~~~~~~~~~~~~~~
262+
- Fixes a regression that prevented async fixtures from working in synchronous tests. `#286 <https://github.com/pytest-dev/pytest-asyncio/issues/286>`_
263+
260264
0.18.0 (22-02-07)
261265
~~~~~~~~~~~~~~~~~~~
262266

pytest_asyncio/plugin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,12 @@ def pytest_pycollect_makeitem(
319319
"""A pytest hook to collect asyncio coroutines."""
320320
if not collector.funcnamefilter(name):
321321
return None
322+
_preprocess_async_fixtures(collector.config, _HOLDER)
322323
if (
323324
_is_coroutine(obj)
324325
or _is_hypothesis_test(obj)
325326
and _hypothesis_test_wraps_coroutine(obj)
326327
):
327-
_preprocess_async_fixtures(collector.config, _HOLDER)
328328
item = pytest.Function.from_parent(collector, name=name)
329329
marker = item.get_closest_marker("asyncio")
330330
if marker is not None:

tests/test_asyncio_fixture.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
from textwrap import dedent
23

34
import pytest
45

@@ -39,3 +40,25 @@ async def fixture_with_params(request):
3940
async def test_fixture_with_params(fixture_with_params):
4041
await asyncio.sleep(0)
4142
assert fixture_with_params % 2 == 0
43+
44+
45+
@pytest.mark.parametrize("mode", ("auto", "strict", "legacy"))
46+
def test_sync_function_uses_async_fixture(testdir, mode):
47+
testdir.makepyfile(
48+
dedent(
49+
"""\
50+
import pytest_asyncio
51+
52+
pytest_plugins = 'pytest_asyncio'
53+
54+
@pytest_asyncio.fixture
55+
async def always_true():
56+
return True
57+
58+
def test_sync_function_uses_async_fixture(always_true):
59+
assert always_true is True
60+
"""
61+
)
62+
)
63+
result = testdir.runpytest(f"--asyncio-mode={mode}")
64+
result.assert_outcomes(passed=1)

0 commit comments

Comments
 (0)