From 77062867614edc827bafc6c94f33f9934168db16 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 16 Dec 2023 17:32:20 +0200 Subject: [PATCH] nodes: fix tracebacks from collection errors are not getting pruned Fix #11710. --- changelog/11710.bugfix.rst | 1 + src/_pytest/nodes.py | 2 +- testing/test_collection.py | 24 ++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 changelog/11710.bugfix.rst diff --git a/changelog/11710.bugfix.rst b/changelog/11710.bugfix.rst new file mode 100644 index 00000000000..4bbf9fa2e7d --- /dev/null +++ b/changelog/11710.bugfix.rst @@ -0,0 +1 @@ +Fixed tracebacks from collection errors not getting pruned. diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py index 183f3c9d958..c942fab9a16 100644 --- a/src/_pytest/nodes.py +++ b/src/_pytest/nodes.py @@ -579,7 +579,7 @@ def _traceback_filter(self, excinfo: ExceptionInfo[BaseException]) -> Traceback: ntraceback = traceback.cut(path=self.path) if ntraceback == traceback: ntraceback = ntraceback.cut(excludepath=tracebackcutdir) - return excinfo.traceback.filter(excinfo) + return ntraceback.filter(excinfo) return excinfo.traceback diff --git a/testing/test_collection.py b/testing/test_collection.py index b2492f7f28f..00f9b032e27 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -16,6 +16,7 @@ from _pytest.pathlib import symlink_or_skip from _pytest.pytester import HookRecorder from _pytest.pytester import Pytester +from _pytest._code.code import ExceptionChainRepr, ReprTraceback def ensure_file(file_path: Path) -> Path: @@ -345,6 +346,29 @@ def pytest_make_collect_report(): result = pytester.runpytest(p) result.stdout.fnmatch_lines(["*ERROR collecting*", "*header1*"]) + def test_collection_error_traceback_is_clean(self, pytester: Pytester) -> None: + """When a collection error occurs, the report traceback doesn't contain + internal pytest stack entries. + + Issue #11710. + """ + pytester.makepyfile( + """ + raise Exception("LOUSY") + """ + ) + result = pytester.runpytest() + result.stdout.fnmatch_lines( + [ + "*ERROR collecting*", + "test_*.py:1: in ", + ' raise Exception("LOUSY")', + "E Exception: LOUSY", + "*= short test summary info =*", + ], + consecutive=True, + ) + class TestCustomConftests: def test_ignore_collect_path(self, pytester: Pytester) -> None: