Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check marker arguments #886

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changelog
===================
- Adds an optional `loop_scope` keyword argument to `pytest.mark.asyncio`. This argument controls which event loop is used to run the marked async test. `#706`_, `#871 <https://github.com/pytest-dev/pytest-asyncio/pull/871>`_
- Deprecates the optional `scope` keyword argument to `pytest.mark.asyncio` for API consistency with ``pytest_asyncio.fixture``. Users are encouraged to use the `loop_scope` keyword argument, which does exactly the same.
- Raises an error when passing `scope` or `loop_scope` as a positional argument to ``@pytest.mark.asyncio``. `#812 <https://github.com/pytest-dev/pytest-asyncio/issues/812>`_


0.23.8 (2024-07-17)
Expand Down
4 changes: 4 additions & 0 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,10 @@ def pytest_runtest_setup(item: pytest.Item) -> None:

def _get_marked_loop_scope(asyncio_marker: Mark) -> _ScopeName:
assert asyncio_marker.name == "asyncio"
if asyncio_marker.args or (
asyncio_marker.kwargs and set(asyncio_marker.kwargs) - {"loop_scope", "scope"}
):
raise ValueError("mark.asyncio accepts only a keyword argument 'scope'.")
if "scope" in asyncio_marker.kwargs:
if "loop_scope" in asyncio_marker.kwargs:
raise pytest.UsageError(_DUPLICATE_LOOP_SCOPE_DEFINITION_ERROR)
Expand Down
85 changes: 85 additions & 0 deletions tests/markers/test_invalid_arguments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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(loop_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(loop_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*"]
)