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 @@ -261,6 +261,7 @@ Junhao Liao
Jurko Gospodnetić
Justice Ndou
Justyna Janczyszyn
jwalith4418-Mj
Kale Kundert
Kamran Ahmad
Kenny Y
Expand Down
1 change: 1 addition & 0 deletions changelog/7692.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Custom test items now respect ``__tracebackhide__`` when reporting failures, unless ``--full-trace`` is used.
2 changes: 1 addition & 1 deletion src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def getparent(self, cls: type[_NodeType]) -> _NodeType | None:
return None

def _traceback_filter(self, excinfo: ExceptionInfo[BaseException]) -> Traceback:
return excinfo.traceback
return excinfo.traceback.filter(excinfo)

def _repr_failure_py(
self,
Expand Down
40 changes: 40 additions & 0 deletions testing/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,43 @@ def test_show_wrong_path(private_dir):
)
result = pytester.runpytest()
result.stdout.fnmatch_lines([str(p) + ":*: AssertionError", "*1 failed in *"])


@pytest.mark.parametrize("fulltrace", [False, True])
@pytest.mark.parametrize(
"failure", ["raise ValueError('custom failure')", "pytest.fail('custom failure')"]
)
def test_custom_item_hidden_traceback(
pytester: Pytester, fulltrace: bool, failure: str
) -> None:
pytester.makeconftest(
f"""
import pytest

def hidden_helper():
__tracebackhide__ = True
{failure}

class CustomItem(pytest.Item):
def runtest(self):
hidden_helper()

class CustomFile(pytest.File):
def collect(self):
yield CustomItem.from_parent(self, name="custom")

def pytest_collect_file(file_path, parent):
if file_path.suffix == ".custom":
return CustomFile.from_parent(parent, path=file_path)
"""
)
pytester.makefile(".custom", test_custom="")
args = ["--tb=long"]
if fulltrace:
args.append("--full-trace")
result = pytester.runpytest(*args)
result.assert_outcomes(failed=1)
output = result.stdout.str()
assert "custom failure" in output
assert "def runtest(self):" in output
assert ("__tracebackhide__ = True" in output) == fulltrace
Loading