From 7f95c9c3d75f7d4801a12e16786529f559a856e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Hofst=C3=A4tter?= Date: Sun, 12 Mar 2023 16:39:26 +0100 Subject: [PATCH] add checks to make sure crash entries are not none where they should not be --- src/_pytest/_code/code.py | 7 ++++++- src/_pytest/reports.py | 4 ++++ testing/code/test_excinfo.py | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 1f08a3c8e..b7ed7586a 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -650,13 +650,18 @@ class ExceptionInfo(Generic[E]): Added the ``chain`` parameter. """ if style == "native": + r = self._getreprcrash() + if r is None: + raise ValueError( + "There should always be a non-hidden traceback entry for the top level function." + ) return ReprExceptionInfo( reprtraceback=ReprTracebackNative( traceback.format_exception( self.type, self.value, self.traceback[0]._rawentry ) ), - reprcrash=self._getreprcrash(), + reprcrash=r, ) fmt = FormattedExcinfo( diff --git a/src/_pytest/reports.py b/src/_pytest/reports.py index 2e36514ea..c0a76f92b 100644 --- a/src/_pytest/reports.py +++ b/src/_pytest/reports.py @@ -347,6 +347,10 @@ class TestReport(BaseReport): elif isinstance(excinfo.value, skip.Exception): outcome = "skipped" r = excinfo._getreprcrash() + if r is None: + raise ValueError( + "There should always be a traceback entry for skipping a test." + ) if excinfo.value._use_item_location: path, line = item.reportinfo()[:2] assert line is not None diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index b102f1006..3607501a8 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -294,6 +294,7 @@ class TestTraceback_f_g_h: excinfo = pytest.raises(ValueError, f) tb = excinfo.traceback entry = tb.getcrashentry() + assert entry is not None co = _pytest._code.Code.from_function(h) assert entry.frame.code.path == co.path assert entry.lineno == co.firstlineno + 1