Skip to content

Commit 67d4fa2

Browse files
committed
make it raise PytestDeprecationWarning instead
1 parent 13cd36b commit 67d4fa2

File tree

2 files changed

+80
-9
lines changed

2 files changed

+80
-9
lines changed

pytest_asyncio/plugin.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ def pytest_pyfunc_call(pyfuncitem: Function) -> object | None:
881881
if pyfuncitem.get_closest_marker("asyncio") is not None:
882882
if isinstance(pyfuncitem, PytestAsyncioFunction):
883883
asyncio_mode = _get_asyncio_mode(pyfuncitem.config)
884-
for fixtures in pyfuncitem._fixtureinfo.name2fixturedefs.values():
884+
for fixname, fixtures in pyfuncitem._fixtureinfo.name2fixturedefs.items():
885885
# name2fixturedefs is a dict between fixture name and a list of matching
886886
# fixturedefs. The last entry in the list is closest and the one used.
887887
func = fixtures[-1].func
@@ -890,10 +890,22 @@ def pytest_pyfunc_call(pyfuncitem: Function) -> object | None:
890890
and not _is_asyncio_fixture_function(func)
891891
and asyncio_mode == Mode.STRICT
892892
):
893-
pytest.fail(
894-
"asyncio test requested async fixture not marked asyncio, "
895-
"in strict mode. You might want to use @pytest_asyncio.fixture"
893+
warnings.warn(
894+
PytestDeprecationWarning(
895+
f"asyncio test {pyfuncitem.name!r} requested async "
896+
"@pytest.fixture "
897+
f"{fixname!r} in strict mode. "
898+
"You might want to use @pytest_asyncio.fixture or switch "
899+
"to auto mode. "
900+
"This will become an error in future versions of "
901+
"flake8-asyncio."
902+
),
903+
stacklevel=1,
896904
)
905+
# no stacklevel points at the users code, so we set stacklevel=1
906+
# so it at least indicates that it's the plugin complaining.
907+
# Pytest gives the test file & name in the warnings summary at least
908+
897909
else:
898910
pyfuncitem.warn(
899911
pytest.PytestWarning(

tests/modes/test_strict_mode.py

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ async def test_anything(any_fixture):
126126
)
127127

128128

129-
def test_strict_mode_marked_test_errors_unmarked_fixture(pytester: Pytester):
129+
def test_strict_mode_marked_test_unmarked_fixture_warning(pytester: Pytester):
130130
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
131131
pytester.makepyfile(
132132
dedent(
@@ -136,19 +136,78 @@ def test_strict_mode_marked_test_errors_unmarked_fixture(pytester: Pytester):
136136
# Not using pytest_asyncio.fixture
137137
@pytest.fixture()
138138
async def any_fixture():
139-
raise RuntimeError()
139+
pass
140140
141141
@pytest.mark.asyncio
142142
async def test_anything(any_fixture):
143+
# suppress unawaited coroutine warning
144+
try:
145+
any_fixture.send(None)
146+
except StopIteration:
147+
pass
148+
"""
149+
)
150+
)
151+
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
152+
result.assert_outcomes(passed=1, failed=0, skipped=0, warnings=1)
153+
result.stdout.fnmatch_lines(
154+
[
155+
"*warnings summary*",
156+
(
157+
"test_strict_mode_marked_test_unmarked_fixture_warning.py::"
158+
"test_anything"
159+
),
160+
(
161+
"*/pytest_asyncio/plugin.py:*: PytestDeprecationWarning: "
162+
"asyncio test 'test_anything' requested async "
163+
"@pytest.fixture 'any_fixture' in strict mode. "
164+
"You might want to use @pytest_asyncio.fixture or switch to "
165+
"auto mode. "
166+
"This will become an error in future versions of flake8-asyncio."
167+
),
168+
],
169+
)
170+
171+
172+
# autouse is not handled in any special way currently
173+
def test_strict_mode_marked_test_unmarked_autouse_fixture_warning(pytester: Pytester):
174+
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
175+
pytester.makepyfile(
176+
dedent(
177+
"""\
178+
import pytest
179+
180+
# Not using pytest_asyncio.fixture
181+
@pytest.fixture(autouse=True)
182+
async def any_fixture():
143183
pass
184+
185+
@pytest.mark.asyncio
186+
async def test_anything(any_fixture):
187+
# suppress unawaited coroutine warning
188+
try:
189+
any_fixture.send(None)
190+
except StopIteration:
191+
pass
144192
"""
145193
)
146194
)
147195
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
148-
result.assert_outcomes(failed=1, skipped=0, warnings=1)
196+
result.assert_outcomes(passed=1, warnings=1)
149197
result.stdout.fnmatch_lines(
150198
[
151-
"*asyncio test requested async fixture not marked asyncio, in strict mode*",
152-
"*coroutine 'any_fixture' was never awaited*",
199+
"*warnings summary*",
200+
(
201+
"test_strict_mode_marked_test_unmarked_autouse_fixture_warning.py::"
202+
"test_anything"
203+
),
204+
(
205+
"*/pytest_asyncio/plugin.py:*: PytestDeprecationWarning: "
206+
"*asyncio test 'test_anything' requested async "
207+
"@pytest.fixture 'any_fixture' in strict mode. "
208+
"You might want to use @pytest_asyncio.fixture or switch to "
209+
"auto mode. "
210+
"This will become an error in future versions of flake8-asyncio."
211+
),
153212
],
154213
)

0 commit comments

Comments
 (0)