MCPcopy Create free account
hub / github.com/pytest-dev/pytest / test_chained_exceptions

Method test_chained_exceptions

testing/test_reports.py:275–348  ·  view source on GitHub ↗

Check serialization/deserialization of report objects containing chained exceptions (#5786)

(
        self, pytester: Pytester, tw_mock, report_class
    )

Source from the content-addressed store, hash-verified

273
274 @pytest.mark.parametrize("report_class", [TestReport, CollectReport])
275 def test_chained_exceptions(
276 self, pytester: Pytester, tw_mock, report_class
277 ) -> None:
278 """Check serialization/deserialization of report objects containing chained exceptions (#5786)"""
279 pytester.makepyfile(
280 """
281 def foo():
282 raise ValueError('value error')
283 def test_a():
284 try:
285 foo()
286 except ValueError as e:
287 raise RuntimeError('runtime error') from e
288 if {error_during_import}:
289 test_a()
290 """.format(
291 error_during_import=report_class is CollectReport
292 )
293 )
294
295 reprec = pytester.inline_run()
296 if report_class is TestReport:
297 reports: Union[
298 Sequence[TestReport], Sequence[CollectReport]
299 ] = reprec.getreports("pytest_runtest_logreport")
300 # we have 3 reports: setup/call/teardown
301 assert len(reports) == 3
302 # get the call report
303 report = reports[1]
304 else:
305 assert report_class is CollectReport
306 # two collection reports: session and test file
307 reports = reprec.getreports("pytest_collectreport")
308 assert len(reports) == 2
309 report = reports[1]
310
311 def check_longrepr(longrepr: ExceptionChainRepr) -> None:
312 """Check the attributes of the given longrepr object according to the test file.
313
314 We can get away with testing both CollectReport and TestReport with this function because
315 the longrepr objects are very similar.
316 """
317 assert isinstance(longrepr, ExceptionChainRepr)
318 assert longrepr.sections == [("title", "contents", "=")]
319 assert len(longrepr.chain) == 2
320 entry1, entry2 = longrepr.chain
321 tb1, fileloc1, desc1 = entry1
322 tb2, fileloc2, desc2 = entry2
323
324 assert "ValueError('value error')" in str(tb1)
325 assert "RuntimeError('runtime error')" in str(tb2)
326
327 assert (
328 desc1
329 == "The above exception was the direct cause of the following exception:"
330 )
331 assert desc2 is None
332

Callers

nothing calls this directly

Calls 8

addsectionMethod · 0.80
_to_jsonMethod · 0.80
_from_jsonMethod · 0.80
makepyfileMethod · 0.45
formatMethod · 0.45
inline_runMethod · 0.45
getreportsMethod · 0.45
toterminalMethod · 0.45

Tested by

no test coverage detected