diff --git a/docs/source/reference/changelog.rst b/docs/source/reference/changelog.rst index e489c12e..8e5e7d08 100644 --- a/docs/source/reference/changelog.rst +++ b/docs/source/reference/changelog.rst @@ -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. diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index c0aa4261..b7c3fcbf 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -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: diff --git a/tests/test_simple.py b/tests/test_simple.py index 7b87a7f4..b91afd4a 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -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." + )