Skip to content

Commit 2b88268

Browse files
committed
Raise error if sync test relies on async fixture, and warn if the fixture is autouse.
1 parent 57b2f3e commit 2b88268

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/_pytest/fixtures.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,10 @@ def _get_active_fixturedef(
595595
raise FixtureLookupError(argname, self)
596596
fixturedef = fixturedefs[index]
597597

598-
if not inspect.iscoroutinefunction(
599-
self.function
600-
) and inspect.iscoroutinefunction(fixturedef.func):
598+
if not inspect.iscoroutinefunction(self.function) and (
599+
inspect.iscoroutinefunction(fixturedef.func)
600+
or inspect.isasyncgenfunction(fixturedef.func)
601+
):
601602
if fixturedef._autouse:
602603
warnings.warn(
603604
PytestRemovedIn9Warning(

testing/acceptance_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,33 @@ def test_foo(async_fixture):
13131313
result.assert_outcomes(errors=1)
13141314

13151315

1316+
def test_error_on_sync_test_async_fixture_gen(pytester: Pytester) -> None:
1317+
pytester.makepyfile(
1318+
test_sync="""
1319+
import pytest
1320+
1321+
@pytest.fixture
1322+
async def async_fixture():
1323+
yield
1324+
1325+
def test_foo(async_fixture):
1326+
...
1327+
"""
1328+
)
1329+
result = pytester.runpytest()
1330+
result.stdout.fnmatch_lines(
1331+
[
1332+
(
1333+
"*ERROR: Sync test requested async fixture. "
1334+
"You may want to make the test asynchronous and run it with "
1335+
"a suitable async framework test plugin.*"
1336+
),
1337+
"*ERROR test_sync.py::test_foo*",
1338+
]
1339+
)
1340+
result.assert_outcomes(errors=1)
1341+
1342+
13161343
def test_warning_on_sync_test_async_autouse_fixture(pytester: Pytester) -> None:
13171344
pytester.makepyfile(
13181345
test_sync="""

0 commit comments

Comments
 (0)