Skip to content

Commit

Permalink
Check marker arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Jul 18, 2024
1 parent 97bd934 commit e2bd784
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,8 @@ def pytest_generate_tests(metafunc: Metafunc) -> None:
marker = metafunc.definition.get_closest_marker("asyncio")
if not marker:
return
if marker.args or (marker.kwargs and list(marker.kwargs) != ["scope"]):
raise ValueError("mark.asyncio accepts only a keyword argument 'scope'.")
scope = marker.kwargs.get("scope", "function")
if scope == "function":
return
Expand Down Expand Up @@ -959,7 +961,10 @@ def _retrieve_scope_root(item: Union[Collector, Item], scope: str) -> Collector:
"package": Package,
"session": Session,
}
scope_root_type = node_type_by_scope[scope]
try:
scope_root_type = node_type_by_scope[scope]
except KeyError as error:
raise ValueError(f"Invalid scope {scope!r}.") from error
for node in reversed(item.listchain()):
if isinstance(node, scope_root_type):
assert isinstance(node, pytest.Collector)
Expand Down
104 changes: 104 additions & 0 deletions tests/markers/test_invalid_arguments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
from textwrap import dedent

import pytest


def test_no_error_when_scope_passed_as_sole_keyword_argument(
pytester: pytest.Pytester,
):
pytester.makepyfile(
dedent(
"""\
import pytest
@pytest.mark.asyncio(scope="session")
async def test_anything():
pass
"""
)
)
result = pytester.runpytest_subprocess()
result.assert_outcomes(passed=1)
result.stdout.no_fnmatch_line("*ValueError*")


def test_error_when_scope_passed_as_positional_argument(
pytester: pytest.Pytester,
):
pytester.makepyfile(
dedent(
"""\
import pytest
@pytest.mark.asyncio("session")
async def test_anything():
pass
"""
)
)
result = pytester.runpytest_subprocess()
result.assert_outcomes(errors=1)
result.stdout.fnmatch_lines(
["*ValueError: mark.asyncio accepts only a keyword argument*"]
)


def test_error_when_wrong_keyword_argument_is_passed(
pytester: pytest.Pytester,
):
pytester.makepyfile(
dedent(
"""\
import pytest
@pytest.mark.asyncio(cope="session")
async def test_anything():
pass
"""
)
)
result = pytester.runpytest_subprocess()
result.assert_outcomes(errors=1)
result.stdout.fnmatch_lines(
["*ValueError: mark.asyncio accepts only a keyword argument 'scope'*"]
)


def test_error_when_additional_keyword_arguments_are_passed(
pytester: pytest.Pytester,
):
pytester.makepyfile(
dedent(
"""\
import pytest
@pytest.mark.asyncio(scope="session", more="stuff")
async def test_anything():
pass
"""
)
)
result = pytester.runpytest_subprocess()
result.assert_outcomes(errors=1)
result.stdout.fnmatch_lines(
["*ValueError: mark.asyncio accepts only a keyword argument*"]
)


def test_error_when_an_invalid_scope_is_passed(
pytester: pytest.Pytester,
):
pytester.makepyfile(
dedent(
"""\
import pytest
@pytest.mark.asyncio(scope="stuff")
async def test_anything():
pass
"""
)
)
result = pytester.runpytest_subprocess()
result.assert_outcomes(errors=1)
result.stdout.fnmatch_lines(["*ValueError: Invalid scope 'stuff'*"])

0 comments on commit e2bd784

Please sign in to comment.