MCPcopy
hub / github.com/pytest-dev/pytest / test_chained_exceptions

Method test_chained_exceptions

testing/test_reports.py:276–347  ·  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

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

Callers

nothing calls this directly

Calls 7

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

Tested by

no test coverage detected