Skip to content
Open
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
1 change: 1 addition & 0 deletions changelog.d/1633.fixed.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
An invalid ``asyncio_mode`` is now reported during configuration, instead of being silently accepted when pytest runs with ``-q``.
4 changes: 4 additions & 0 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ def _validate_scope(scope: str | None, option_name: str) -> None:


def pytest_configure(config: Config) -> None:
# Validate the mode eagerly so that an invalid value fails here,
# even when `-q` suppresses the report header (where the mode
# would otherwise first be read).
_get_asyncio_mode(config)
default_fixture_loop_scope = config.getini("asyncio_default_fixture_loop_scope")
if default_fixture_loop_scope != _MATCH_FIXTURE_SCOPE:
_validate_scope(
Expand Down
42 changes: 42 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,45 @@ def test_invalid_asyncio_mode(pytester):
result.stderr.fnmatch_lines(
"ERROR: 'True' is not a valid asyncio_mode. Valid modes: auto, strict."
)


@pytest.mark.parametrize(
"mode_args",
[
["-o", "asyncio_mode=bogus"],
["--asyncio-mode=bogus"],
],
)
def test_invalid_asyncio_mode_quiet(pytester, mode_args):
"""
Invalid asyncio_mode errors in quiet mode with sync-only tests.

With `-q` the report header is suppressed, so the mode used to be
silently accepted when no async test was collected.
"""
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile("def test_sync():\n pass\n")
result = pytester.runpytest("-q", *mode_args)
assert result.ret == 4
result.stderr.no_fnmatch_line("INTERNALERROR> *")
result.stderr.fnmatch_lines(
"ERROR: 'bogus' is not a valid asyncio_mode. Valid modes: auto, strict."
)


def test_invalid_asyncio_mode_quiet_with_async_test(pytester):
"""Invalid asyncio_mode fails at configuration time, not per test."""
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(dedent("""\
import pytest

@pytest.mark.asyncio
async def test_async():
pass
"""))
result = pytester.runpytest("-q", "-o", "asyncio_mode=bogus")
assert result.ret == 4
result.stderr.no_fnmatch_line("INTERNALERROR> *")
result.stderr.fnmatch_lines(
"ERROR: 'bogus' is not a valid asyncio_mode. Valid modes: auto, strict."
)
Loading