From 62d32fccf9625676afa52020d0426ac4b2c5e187 Mon Sep 17 00:00:00 2001 From: jwalith4418-Mj Date: Sun, 25 Oct 2026 03:07:59 +0530 Subject: [PATCH 1/2] fix: respect hidden traceback frames for custom items Custom items inherit Node's default traceback filter, which returned the traceback without applying __tracebackhide__. Use the existing filter so marked helper and internal frames are omitted unless full tracebacks are explicitly requested. Cover ValueError and pytest.fail through a public custom collector/item, with and without --full-trace. Add the changelog and contributor entry. Fixes #7692. Co-authored-by: OpenAI Codex --- AUTHORS | 1 + changelog/7692.bugfix.rst | 1 + src/_pytest/nodes.py | 2 +- testing/test_nodes.py | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 changelog/7692.bugfix.rst diff --git a/AUTHORS b/AUTHORS index 1d1af001595..0860485c9f2 100644 --- a/AUTHORS +++ b/AUTHORS @@ -261,6 +261,7 @@ Junhao Liao Jurko Gospodnetić Justice Ndou Justyna Janczyszyn +jwalith4418-Mj Kale Kundert Kamran Ahmad Kenny Y diff --git a/changelog/7692.bugfix.rst b/changelog/7692.bugfix.rst new file mode 100644 index 00000000000..192f24058a9 --- /dev/null +++ b/changelog/7692.bugfix.rst @@ -0,0 +1 @@ +Custom test items now respect ``__tracebackhide__`` when reporting failures, unless ``--full-trace`` is used. diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py index 2b510907122..19c1e98b490 100644 --- a/src/_pytest/nodes.py +++ b/src/_pytest/nodes.py @@ -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, diff --git a/testing/test_nodes.py b/testing/test_nodes.py index e976b9e6f11..9037b21a5ed 100644 --- a/testing/test_nodes.py +++ b/testing/test_nodes.py @@ -162,3 +162,42 @@ 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 ("__tracebackhide__ = True" in output) == fulltrace From c1dc52438007d0621cc5f38b0d04f0bf46fa3075 Mon Sep 17 00:00:00 2001 From: jwalith4418-Mj Date: Tue, 27 Oct 2026 19:22:50 +0530 Subject: [PATCH 2/2] Assert unmarked custom item traceback frames remain visible Check the runtest frame in all four existing hidden-frame regression cases, including both failure paths and --full-trace settings. Co-authored-by: OpenAI Codex --- testing/test_nodes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/testing/test_nodes.py b/testing/test_nodes.py index 9037b21a5ed..5bf56794824 100644 --- a/testing/test_nodes.py +++ b/testing/test_nodes.py @@ -200,4 +200,5 @@ def pytest_collect_file(file_path, parent): 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