Skip to content

Commit 7b4cfe5

Browse files
Citoseifertm
authored andcommitted
feat: Check marker arguments
1 parent a8b3d18 commit 7b4cfe5

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

pytest_asyncio/plugin.py

+2
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,8 @@ def pytest_runtest_setup(item: pytest.Item) -> None:
10041004

10051005
def _get_marked_loop_scope(asyncio_marker: Mark) -> _ScopeName:
10061006
assert asyncio_marker.name == "asyncio"
1007+
if asyncio_marker.args or (asyncio_marker.kwargs and set(asyncio_marker.kwargs) - {"loop_scope", "scope"}):
1008+
raise ValueError("mark.asyncio accepts only a keyword argument 'scope'.")
10071009
if "scope" in asyncio_marker.kwargs:
10081010
if "loop_scope" in asyncio_marker.kwargs:
10091011
raise pytest.UsageError(_DUPLICATE_LOOP_SCOPE_DEFINITION_ERROR)
+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from textwrap import dedent
2+
3+
import pytest
4+
5+
6+
def test_no_error_when_scope_passed_as_sole_keyword_argument(
7+
pytester: pytest.Pytester,
8+
):
9+
pytester.makepyfile(
10+
dedent(
11+
"""\
12+
import pytest
13+
14+
@pytest.mark.asyncio(loop_scope="session")
15+
async def test_anything():
16+
pass
17+
"""
18+
)
19+
)
20+
result = pytester.runpytest_subprocess()
21+
result.assert_outcomes(passed=1)
22+
result.stdout.no_fnmatch_line("*ValueError*")
23+
24+
25+
def test_error_when_scope_passed_as_positional_argument(
26+
pytester: pytest.Pytester,
27+
):
28+
pytester.makepyfile(
29+
dedent(
30+
"""\
31+
import pytest
32+
33+
@pytest.mark.asyncio("session")
34+
async def test_anything():
35+
pass
36+
"""
37+
)
38+
)
39+
result = pytester.runpytest_subprocess()
40+
result.assert_outcomes(errors=1)
41+
result.stdout.fnmatch_lines(
42+
["*ValueError: mark.asyncio accepts only a keyword argument*"]
43+
)
44+
45+
46+
def test_error_when_wrong_keyword_argument_is_passed(
47+
pytester: pytest.Pytester,
48+
):
49+
pytester.makepyfile(
50+
dedent(
51+
"""\
52+
import pytest
53+
54+
@pytest.mark.asyncio(cope="session")
55+
async def test_anything():
56+
pass
57+
"""
58+
)
59+
)
60+
result = pytester.runpytest_subprocess()
61+
result.assert_outcomes(errors=1)
62+
result.stdout.fnmatch_lines(
63+
["*ValueError: mark.asyncio accepts only a keyword argument 'scope'*"]
64+
)
65+
66+
67+
def test_error_when_additional_keyword_arguments_are_passed(
68+
pytester: pytest.Pytester,
69+
):
70+
pytester.makepyfile(
71+
dedent(
72+
"""\
73+
import pytest
74+
75+
@pytest.mark.asyncio(loop_scope="session", more="stuff")
76+
async def test_anything():
77+
pass
78+
"""
79+
)
80+
)
81+
result = pytester.runpytest_subprocess()
82+
result.assert_outcomes(errors=1)
83+
result.stdout.fnmatch_lines(
84+
["*ValueError: mark.asyncio accepts only a keyword argument*"]
85+
)

0 commit comments

Comments
 (0)