Skip to content

Commit e41af6c

Browse files
authored
Improve output with invalid asyncio_mode (#557)
Instead of a long INTERNALERROR> stack trace, output a proper error message: $ pytest -o asyncio_mode=True =================== test session starts ==================== platform linux -- Python 3.11.3, pytest-7.3.1, pluggy-1.0.0 ERROR: 'True' is not a valid asyncio_mode. Valid modes: auto, strict. See pytest-dev/pytest#11083
1 parent f0cdf46 commit e41af6c

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

docs/source/reference/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
Changelog
33
=========
44

5+
0.22.0 (UNRELEASED)
6+
===================
7+
- Output a proper error message when an invalid ``asyncio_mode`` is selected.
8+
59
0.21.0 (2023-03-19)
610
===================
711
- Drop compatibility with pytest 6.1. Pytest-asyncio now depends on pytest 7.0 or newer.

pytest_asyncio/plugin.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,13 @@ def _get_asyncio_mode(config: Config) -> Mode:
164164
val = config.getoption("asyncio_mode")
165165
if val is None:
166166
val = config.getini("asyncio_mode")
167-
return Mode(val)
167+
try:
168+
return Mode(val)
169+
except ValueError:
170+
modes = ", ".join(m.value for m in Mode)
171+
raise pytest.UsageError(
172+
f"{val!r} is not a valid asyncio_mode. Valid modes: {modes}."
173+
)
168174

169175

170176
def pytest_configure(config: Config) -> None:

tests/test_simple.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,11 @@ def test_a():
272272
result.stdout.fnmatch_lines(
273273
["*is marked with '@pytest.mark.asyncio' but it is not an async function.*"]
274274
)
275+
276+
277+
def test_invalid_asyncio_mode(testdir):
278+
result = testdir.runpytest("-o", "asyncio_mode=True")
279+
result.stderr.no_fnmatch_line("INTERNALERROR> *")
280+
result.stderr.fnmatch_lines(
281+
"ERROR: 'True' is not a valid asyncio_mode. Valid modes: auto, strict."
282+
)

0 commit comments

Comments
 (0)