Skip to content

Commit d00f908

Browse files
pankaj-bindseifertm
authored andcommitted
feat: Add validation for asyncio_default_fixture_loop_scope and asyncio_default_test_loop_scope
fix linters issue added changelog entry [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci docs: Reword changelog entry.
1 parent b398244 commit d00f908

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

changelog.d/1189.added.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A ``pytest.UsageError`` for invalid configuration values of ``asyncio_default_fixture_loop_scope`` and ``asyncio_default_test_loop_scope``.

pytest_asyncio/plugin.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,25 @@ def _get_asyncio_debug(config: Config) -> bool:
234234
"""
235235

236236

237+
def _validate_scope(scope: str | None, option_name: str) -> None:
238+
if scope is None:
239+
return
240+
valid_scopes = [s.value for s in Scope]
241+
if scope not in valid_scopes:
242+
raise pytest.UsageError(
243+
f"{scope!r} is not a valid {option_name}. "
244+
f"Valid scopes are: {', '.join(valid_scopes)}."
245+
)
246+
247+
237248
def pytest_configure(config: Config) -> None:
238-
default_loop_scope = config.getini("asyncio_default_fixture_loop_scope")
239-
if not default_loop_scope:
249+
default_fixture_loop_scope = config.getini("asyncio_default_fixture_loop_scope")
250+
_validate_scope(default_fixture_loop_scope, "asyncio_default_fixture_loop_scope")
251+
if not default_fixture_loop_scope:
240252
warnings.warn(PytestDeprecationWarning(_DEFAULT_FIXTURE_LOOP_SCOPE_UNSET))
253+
254+
default_test_loop_scope = config.getini("asyncio_default_test_loop_scope")
255+
_validate_scope(default_test_loop_scope, "asyncio_default_test_loop_scope")
241256
config.addinivalue_line(
242257
"markers",
243258
"asyncio: "

tests/test_fixture_loop_scopes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,20 @@ async def test_runs_in_fixture_loop(fixture_loop):
136136
)
137137
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
138138
result.assert_outcomes(passed=1)
139+
140+
141+
def test_invalid_default_fixture_loop_scope_raises_error(pytester: Pytester):
142+
pytester.makeini(
143+
"""\
144+
[pytest]
145+
asyncio_default_fixture_loop_scope = invalid_scope
146+
"""
147+
)
148+
result = pytester.runpytest()
149+
result.stderr.fnmatch_lines(
150+
[
151+
"ERROR: 'invalid_scope' is not a valid "
152+
"asyncio_default_fixture_loop_scope. Valid scopes are: "
153+
"function, class, module, package, session."
154+
]
155+
)

0 commit comments

Comments
 (0)