Skip to content

Commit

Permalink
Improve output with invalid asyncio_mode (#557)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
The-Compiler committed Jun 7, 2023
1 parent f0cdf46 commit e41af6c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/source/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Changelog
=========

0.22.0 (UNRELEASED)
===================
- Output a proper error message when an invalid ``asyncio_mode`` is selected.

0.21.0 (2023-03-19)
===================
- Drop compatibility with pytest 6.1. Pytest-asyncio now depends on pytest 7.0 or newer.
Expand Down
8 changes: 7 additions & 1 deletion pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,13 @@ def _get_asyncio_mode(config: Config) -> Mode:
val = config.getoption("asyncio_mode")
if val is None:
val = config.getini("asyncio_mode")
return Mode(val)
try:
return Mode(val)
except ValueError:
modes = ", ".join(m.value for m in Mode)
raise pytest.UsageError(
f"{val!r} is not a valid asyncio_mode. Valid modes: {modes}."
)


def pytest_configure(config: Config) -> None:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,11 @@ def test_a():
result.stdout.fnmatch_lines(
["*is marked with '@pytest.mark.asyncio' but it is not an async function.*"]
)


def test_invalid_asyncio_mode(testdir):
result = testdir.runpytest("-o", "asyncio_mode=True")
result.stderr.no_fnmatch_line("INTERNALERROR> *")
result.stderr.fnmatch_lines(
"ERROR: 'True' is not a valid asyncio_mode. Valid modes: auto, strict."
)

0 comments on commit e41af6c

Please sign in to comment.