Skip to content

Commit c14a9ad

Browse files
authored
Fix skip signature (#8392)
* Fix test_strict_and_skip The `--strict` argument was removed in #2552, but the removal wasn't actually correct - see #1472. * Fix argument handling in pytest.mark.skip See #8384 * Raise from None * Fix test name
1 parent 4dbb294 commit c14a9ad

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

changelog/8384.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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).

doc/en/reference.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pytest.mark.skip
150150

151151
Unconditionally skip a test function.
152152

153-
.. py:function:: pytest.mark.skip(*, reason=None)
153+
.. py:function:: pytest.mark.skip(reason=None)
154154
155155
:keyword str reason: Reason why the test function is being skipped.
156156

src/_pytest/skipping.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def evaluate_condition(item: Item, mark: Mark, condition: object) -> Tuple[bool,
161161
class Skip:
162162
"""The result of evaluate_skip_marks()."""
163163

164-
reason = attr.ib(type=str)
164+
reason = attr.ib(type=str, default="unconditional skip")
165165

166166

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

186186
for mark in item.iter_markers(name="skip"):
187-
if "reason" in mark.kwargs:
188-
reason = mark.kwargs["reason"]
189-
elif mark.args:
190-
reason = mark.args[0]
191-
else:
192-
reason = "unconditional skip"
193-
return Skip(reason)
187+
try:
188+
return Skip(*mark.args, **mark.kwargs)
189+
except TypeError as e:
190+
raise TypeError(str(e) + " - maybe you meant pytest.mark.skipif?") from None
194191

195192
return None
196193

testing/test_skipping.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -861,9 +861,25 @@ def test_hello():
861861
pass
862862
"""
863863
)
864-
result = pytester.runpytest("-rs")
864+
result = pytester.runpytest("-rs", "--strict-markers")
865865
result.stdout.fnmatch_lines(["*unconditional skip*", "*1 skipped*"])
866866

867+
def test_wrong_skip_usage(self, pytester: Pytester) -> None:
868+
pytester.makepyfile(
869+
"""
870+
import pytest
871+
@pytest.mark.skip(False, reason="I thought this was skipif")
872+
def test_hello():
873+
pass
874+
"""
875+
)
876+
result = pytester.runpytest()
877+
result.stdout.fnmatch_lines(
878+
[
879+
"*TypeError: __init__() got multiple values for argument 'reason' - maybe you meant pytest.mark.skipif?"
880+
]
881+
)
882+
867883

868884
class TestSkipif:
869885
def test_skipif_conditional(self, pytester: Pytester) -> None:

0 commit comments

Comments
 (0)