Skip to content

Commit 5eb0c2d

Browse files
committed
Strip pluggy/_pytest frames from --tb=native tracebacks in tests
Our CI runs with --tb=native, which ignores pytest's usual traceback filtering and shows dozens of pluggy/_pytest hook frames above the actual test. Re-apply that filtering in a makereport hook by rebuilding the traceback from only the frames pytest would have kept. https://claude.ai/code/session_017i7NGpHyr4AyhSTcrvFoqZ
1 parent 01ffbea commit 5eb0c2d

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

hypothesis/tests/conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
import time as time_module
1818
from functools import wraps
1919
from pathlib import Path
20+
from types import TracebackType
2021

2122
import pytest
23+
from _pytest._code.code import Traceback
2224
from _pytest.monkeypatch import MonkeyPatch
2325

2426
from hypothesis import is_hypothesis_test, settings
@@ -210,6 +212,28 @@ def _unfreezer(*_):
210212
independent_random = random.Random()
211213

212214

215+
@pytest.hookimpl(hookwrapper=True)
216+
def pytest_runtest_makereport(item, call):
217+
# We run CI with --tb=native, which (unlike pytest's own styles) doesn't
218+
# filter out the dozens of pluggy/_pytest hook frames above the test, so
219+
# every failure is buried in noise. Re-apply pytest's filtering by
220+
# rebuilding the traceback from just the frames it would have kept.
221+
excinfo = call.excinfo
222+
if (
223+
excinfo is not None
224+
and item.config.option.tbstyle == "native"
225+
and hasattr(item, "_traceback_filter")
226+
):
227+
new_tb = None
228+
for entry in reversed(list(item._traceback_filter(excinfo))):
229+
raw = entry._rawentry
230+
new_tb = TracebackType(new_tb, raw.tb_frame, raw.tb_lasti, raw.tb_lineno)
231+
if new_tb is not None:
232+
excinfo.value.__traceback__ = new_tb
233+
excinfo.traceback = Traceback(new_tb)
234+
yield
235+
236+
213237
@pytest.hookimpl(hookwrapper=True)
214238
def pytest_runtest_call(item):
215239
if item.config.getoption("--hypothesis-benchmark-shrinks"):

0 commit comments

Comments
 (0)