Skip to content
Open
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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Aaron Coleman
Abdeali JK
Abdelrahman Elbehery
Abhijeet Kasurde
ace2016
Adam Johnson
Adam Stewart
Adam Uhlir
Expand Down
1 change: 1 addition & 0 deletions changelog/9436.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoided formatting tracebacks for tests marked with ``xfail(run=False)``.
4 changes: 2 additions & 2 deletions src/_pytest/skipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def pytest_runtest_setup(item: Item) -> None:

item.stash[xfailed_key] = xfailed = evaluate_xfail_marks(item)
if xfailed and not item.config.option.runxfail and not xfailed.run:
xfail("[NOTRUN] " + xfailed.reason)
raise xfail.Exception("[NOTRUN] " + xfailed.reason, pytrace=False)


@hookimpl(wrapper=True)
Expand All @@ -262,7 +262,7 @@ def pytest_runtest_call(item: Item) -> Generator[None]:
item.stash[xfailed_key] = xfailed = evaluate_xfail_marks(item)

if xfailed and not item.config.option.runxfail and not xfailed.run:
xfail("[NOTRUN] " + xfailed.reason)
raise xfail.Exception("[NOTRUN] " + xfailed.reason, pytrace=False)

try:
return (yield)
Expand Down
27 changes: 27 additions & 0 deletions testing/test_skipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import textwrap

from _pytest._code import ExceptionInfo
from _pytest.pytester import Pytester
from _pytest.runner import runtestprotocol
from _pytest.skipping import evaluate_skip_marks
Expand Down Expand Up @@ -453,6 +454,32 @@ def test_this_false():
]
)

def test_xfail_not_run_does_not_format_traceback(
self, pytester: Pytester, monkeypatch: pytest.MonkeyPatch
) -> None:
item = pytester.getitem(
"""
import pytest

@pytest.mark.xfail(run=False, reason="noway")
def test_func():
assert 0
"""
)
getrepr = ExceptionInfo.getrepr
styles = []

def spy_getrepr(self, *args, **kwargs):
styles.append(kwargs["style"])
return getrepr(self, *args, **kwargs)

monkeypatch.setattr(ExceptionInfo, "getrepr", spy_getrepr)

reports = runtestprotocol(item, log=False)

assert reports[0].skipped
assert styles == ["value"]

def test_xfail_not_run_no_setup_run(self, pytester: Pytester) -> None:
p = pytester.makepyfile(
test_one="""
Expand Down