Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

warn if asyncio test requests async pytest fixture in strict mode #979

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Changelog
=========

0.25.0 (UNRELEASED)
===================
- Deprecated: Added warning when asyncio test requests async ``@pytest.fixture`` in strict mode. This will become an error in a future version of flake8-asyncio. `#979 <https://github.com/pytest-dev/pytest-asyncio/pull/979>`_

0.24.0 (2024-08-22)
===================
- BREAKING: Updated minimum supported pytest version to v8.2.0
Expand Down
27 changes: 26 additions & 1 deletion pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,32 @@ def pytest_pyfunc_call(pyfuncitem: Function) -> object | None:
"""
if pyfuncitem.get_closest_marker("asyncio") is not None:
if isinstance(pyfuncitem, PytestAsyncioFunction):
pass
asyncio_mode = _get_asyncio_mode(pyfuncitem.config)
for fixname, fixtures in pyfuncitem._fixtureinfo.name2fixturedefs.items():
# name2fixturedefs is a dict between fixture name and a list of matching
# fixturedefs. The last entry in the list is closest and the one used.
func = fixtures[-1].func
if (
_is_coroutine_or_asyncgen(func)
and not _is_asyncio_fixture_function(func)
and asyncio_mode == Mode.STRICT
):
warnings.warn(
PytestDeprecationWarning(
f"asyncio test {pyfuncitem.name!r} requested async "
"@pytest.fixture "
f"{fixname!r} in strict mode. "
"You might want to use @pytest_asyncio.fixture or switch "
"to auto mode. "
"This will become an error in future versions of "
"flake8-asyncio."
),
stacklevel=1,
)
# no stacklevel points at the users code, so we set stacklevel=1
# so it at least indicates that it's the plugin complaining.
# Pytest gives the test file & name in the warnings summary at least

else:
pyfuncitem.warn(
pytest.PytestWarning(
Expand Down
87 changes: 87 additions & 0 deletions tests/modes/test_strict_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,90 @@ async def test_anything(any_fixture):
"*coroutine 'any_fixture' was never awaited*",
],
)


def test_strict_mode_marked_test_unmarked_fixture_warning(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
import pytest

# Not using pytest_asyncio.fixture
@pytest.fixture()
async def any_fixture():
pass

@pytest.mark.asyncio
async def test_anything(any_fixture):
# suppress unawaited coroutine warning
try:
any_fixture.send(None)
except StopIteration:
pass
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(passed=1, failed=0, skipped=0, warnings=1)
result.stdout.fnmatch_lines(
[
"*warnings summary*",
(
"test_strict_mode_marked_test_unmarked_fixture_warning.py::"
"test_anything"
),
(
"*/pytest_asyncio/plugin.py:*: PytestDeprecationWarning: "
"asyncio test 'test_anything' requested async "
"@pytest.fixture 'any_fixture' in strict mode. "
"You might want to use @pytest_asyncio.fixture or switch to "
"auto mode. "
"This will become an error in future versions of flake8-asyncio."
),
],
)


# autouse is not handled in any special way currently
def test_strict_mode_marked_test_unmarked_autouse_fixture_warning(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
import pytest

# Not using pytest_asyncio.fixture
@pytest.fixture(autouse=True)
async def any_fixture():
pass

@pytest.mark.asyncio
async def test_anything(any_fixture):
# suppress unawaited coroutine warning
try:
any_fixture.send(None)
except StopIteration:
pass
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(passed=1, warnings=1)
result.stdout.fnmatch_lines(
[
"*warnings summary*",
(
"test_strict_mode_marked_test_unmarked_autouse_fixture_warning.py::"
"test_anything"
),
(
"*/pytest_asyncio/plugin.py:*: PytestDeprecationWarning: "
"*asyncio test 'test_anything' requested async "
"@pytest.fixture 'any_fixture' in strict mode. "
"You might want to use @pytest_asyncio.fixture or switch to "
"auto mode. "
"This will become an error in future versions of flake8-asyncio."
),
],
)