Skip to content

Commit 6665a79

Browse files
Merge pull request #15367 from neutrinoceros/enh/no-warning-skip-w-kwarg-reason
ENH: allow suspicious-looking reasons in skip when passed as keyword without warning
2 parents 36eed6b + 35a6af5 commit 6665a79

2 files changed

Lines changed: 38 additions & 13 deletions

File tree

‎src/_pytest/skipping.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,23 @@ def evaluate_skip_marks(item: Item) -> Skip | None:
204204
return Skip(reason)
205205

206206
for mark in item.iter_markers(name="skip"):
207-
reason = mark.args[0] if mark.args else mark.kwargs.get("reason")
208207
# Marks applied to a module or class are seen by each of their items,
209208
# so only warn once per reason.
210209
warned_reasons = item.config.stash.setdefault(warned_skip_reasons_key, set())
211210
if (
212-
isinstance(reason, str)
211+
"reason" not in mark.kwargs
212+
and mark.args
213+
and isinstance(reason := mark.args[0], str)
213214
and _looks_like_condition(reason)
214215
and reason not in warned_reasons
215216
):
216217
warned_reasons.add(reason)
217218
item.warn(
218219
PytestWarning(
219220
f"The reason {reason!r} of pytest.mark.skip looks like a condition. "
220-
"Did you mean pytest.mark.skipif?"
221+
"Did you mean pytest.mark.skipif ?\n"
222+
"If intentional, pass reason as a keyword argument to disable "
223+
"this warning."
221224
)
222225
)
223226
try:

‎testing/test_skipping.py‎

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,16 +1569,17 @@ def test_exit_reason_only():
15691569
result.stdout.fnmatch_lines("*_pytest.outcomes.Exit: foo*")
15701570

15711571

1572-
@pytest.mark.parametrize(
1573-
"reason",
1574-
[
1575-
"sys.version_info < (3, 6)",
1576-
"sys.platform.startswith('win')",
1577-
"os.name == 'nt'",
1578-
],
1579-
)
1572+
SUSPECT_REASONS = [
1573+
"sys.version_info < (3, 6)",
1574+
"sys.platform.startswith('win')",
1575+
"os.name == 'nt'",
1576+
]
1577+
1578+
1579+
@pytest.mark.parametrize("reason", SUSPECT_REASONS)
15801580
def test_skip_reason_looks_like_condition_warns(
1581-
pytester: Pytester, reason: str
1581+
pytester: Pytester,
1582+
reason: str,
15821583
) -> None:
15831584
pytester.makepyfile(
15841585
f"""
@@ -1591,11 +1592,32 @@ def test_func():
15911592
)
15921593
result = pytester.runpytest("-W", "default::pytest.PytestWarning")
15931594
result.stdout.fnmatch_lines(
1594-
["*looks like a condition*Did you mean pytest.mark.skipif?"]
1595+
[
1596+
"*looks like a condition*Did you mean pytest.mark.skipif ?",
1597+
"*If intentional, pass reason as a keyword argument to disable this warning.",
1598+
]
15951599
)
15961600
result.assert_outcomes(skipped=1, warnings=1)
15971601

15981602

1603+
@pytest.mark.parametrize("reason", SUSPECT_REASONS)
1604+
def test_skip_reason_looks_like_condition_kwarg(
1605+
pytester: Pytester,
1606+
reason: str,
1607+
) -> None:
1608+
pytester.makepyfile(
1609+
f"""
1610+
import pytest
1611+
1612+
@pytest.mark.skip({reason=!r})
1613+
def test_func():
1614+
pass
1615+
"""
1616+
)
1617+
result = pytester.runpytest("-W", "default::pytest.PytestWarning")
1618+
result.assert_outcomes(skipped=1, warnings=0)
1619+
1620+
15991621
@pytest.mark.parametrize(
16001622
"reason",
16011623
[

0 commit comments

Comments
 (0)