Skip to content

Fix skip signature #8392

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

Merged
merged 4 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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 changelog/8384.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The ``@pytest.mark.skip`` decorator now correctly handles its arguments. When the ``reason`` argument is accidentally given both positional and as a keyword (e.g. because it was confused with ``skipif``), a ``TypeError`` now occurs. Before, such tests were silently skipped, and the positional argument ignored. Additionally, ``reason`` is now documented correctly as positional or keyword (rather than keyword-only).
2 changes: 1 addition & 1 deletion doc/en/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pytest.mark.skip

Unconditionally skip a test function.

.. py:function:: pytest.mark.skip(*, reason=None)
.. py:function:: pytest.mark.skip(reason=None)

:keyword str reason: Reason why the test function is being skipped.

Expand Down
13 changes: 5 additions & 8 deletions src/_pytest/skipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def evaluate_condition(item: Item, mark: Mark, condition: object) -> Tuple[bool,
class Skip:
"""The result of evaluate_skip_marks()."""

reason = attr.ib(type=str)
reason = attr.ib(type=str, default="unconditional skip")


def evaluate_skip_marks(item: Item) -> Optional[Skip]:
Expand All @@ -184,13 +184,10 @@ def evaluate_skip_marks(item: Item) -> Optional[Skip]:
return Skip(reason)

for mark in item.iter_markers(name="skip"):
if "reason" in mark.kwargs:
reason = mark.kwargs["reason"]
elif mark.args:
reason = mark.args[0]
else:
reason = "unconditional skip"
return Skip(reason)
try:
return Skip(*mark.args, **mark.kwargs)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the existing __init__ rather than hand-rolling argument parsing will also catch other issues, like giving too many arguments to the mark. All those were silently ignored before.

except TypeError as e:
raise TypeError(str(e) + " - maybe you meant pytest.mark.skipif?")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not doing "from e" here because it's not really relevant to the user where the exception comes from.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would from None Make sense to completely suppress it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable - added!


return None

Expand Down
18 changes: 17 additions & 1 deletion testing/test_skipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,9 +861,25 @@ def test_hello():
pass
"""
)
result = pytester.runpytest("-rs")
result = pytester.runpytest("-rs", "--strict-markers")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not directly related, see #2552 (comment)

result.stdout.fnmatch_lines(["*unconditional skip*", "*1 skipped*"])

def test_wrong_strict_usage(self, pytester: Pytester) -> None:
pytester.makepyfile(
"""
import pytest
@pytest.mark.skip(False, reason="I thought this was skipif")
def test_hello():
pass
"""
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(
[
"*TypeError: __init__() got multiple values for argument 'reason' - maybe you meant pytest.mark.skipif?"
]
)


class TestSkipif:
def test_skipif_conditional(self, pytester: Pytester) -> None:
Expand Down