33
44import py
55
6+ from _pytest ._code .code import ExceptionChainRepr
67from _pytest ._code .code import ExceptionInfo
78from _pytest ._code .code import ReprEntry
89from _pytest ._code .code import ReprEntryNative
@@ -160,7 +161,7 @@ def _to_json(self):
160161
161162 Experimental method.
162163 """
163- return _test_report_to_json (self )
164+ return _report_to_json (self )
164165
165166 @classmethod
166167 def _from_json (cls , reportdict ):
@@ -172,7 +173,7 @@ def _from_json(cls, reportdict):
172173
173174 Experimental method.
174175 """
175- kwargs = _test_report_kwargs_from_json (reportdict )
176+ kwargs = _report_kwargs_from_json (reportdict )
176177 return cls (** kwargs )
177178
178179
@@ -340,7 +341,7 @@ def pytest_report_from_serializable(data):
340341 )
341342
342343
343- def _test_report_to_json ( test_report ):
344+ def _report_to_json ( report ):
344345 """
345346 This was originally the serialize_report() function from xdist (ca03269).
346347
@@ -366,22 +367,35 @@ def serialize_repr_crash(reprcrash):
366367 return reprcrash .__dict__ .copy ()
367368
368369 def serialize_longrepr (rep ):
369- return {
370+ result = {
370371 "reprcrash" : serialize_repr_crash (rep .longrepr .reprcrash ),
371372 "reprtraceback" : serialize_repr_traceback (rep .longrepr .reprtraceback ),
372373 "sections" : rep .longrepr .sections ,
373374 }
375+ if isinstance (rep .longrepr , ExceptionChainRepr ):
376+ result ["chain" ] = []
377+ for repr_traceback , repr_crash , description in rep .longrepr .chain :
378+ result ["chain" ].append (
379+ (
380+ serialize_repr_traceback (repr_traceback ),
381+ serialize_repr_crash (repr_crash ),
382+ description ,
383+ )
384+ )
385+ else :
386+ result ["chain" ] = None
387+ return result
374388
375- d = test_report .__dict__ .copy ()
376- if hasattr (test_report .longrepr , "toterminal" ):
377- if hasattr (test_report .longrepr , "reprtraceback" ) and hasattr (
378- test_report .longrepr , "reprcrash"
389+ d = report .__dict__ .copy ()
390+ if hasattr (report .longrepr , "toterminal" ):
391+ if hasattr (report .longrepr , "reprtraceback" ) and hasattr (
392+ report .longrepr , "reprcrash"
379393 ):
380- d ["longrepr" ] = serialize_longrepr (test_report )
394+ d ["longrepr" ] = serialize_longrepr (report )
381395 else :
382- d ["longrepr" ] = str (test_report .longrepr )
396+ d ["longrepr" ] = str (report .longrepr )
383397 else :
384- d ["longrepr" ] = test_report .longrepr
398+ d ["longrepr" ] = report .longrepr
385399 for name in d :
386400 if isinstance (d [name ], (py .path .local , Path )):
387401 d [name ] = str (d [name ])
@@ -390,12 +404,11 @@ def serialize_longrepr(rep):
390404 return d
391405
392406
393- def _test_report_kwargs_from_json (reportdict ):
407+ def _report_kwargs_from_json (reportdict ):
394408 """
395409 This was originally the serialize_report() function from xdist (ca03269).
396410
397- Factory method that returns either a TestReport or CollectReport, depending on the calling
398- class. It's the callers responsibility to know which class to pass here.
411+ Returns **kwargs that can be used to construct a TestReport or CollectReport instance.
399412 """
400413
401414 def deserialize_repr_entry (entry_data ):
@@ -439,12 +452,26 @@ def deserialize_repr_crash(repr_crash_dict):
439452 and "reprcrash" in reportdict ["longrepr" ]
440453 and "reprtraceback" in reportdict ["longrepr" ]
441454 ):
442- exception_info = ReprExceptionInfo (
443- reprtraceback = deserialize_repr_traceback (
444- reportdict ["longrepr" ]["reprtraceback" ]
445- ),
446- reprcrash = deserialize_repr_crash (reportdict ["longrepr" ]["reprcrash" ]),
455+
456+ reprtraceback = deserialize_repr_traceback (
457+ reportdict ["longrepr" ]["reprtraceback" ]
447458 )
459+ reprcrash = deserialize_repr_crash (reportdict ["longrepr" ]["reprcrash" ])
460+ if reportdict ["longrepr" ]["chain" ]:
461+ chain = []
462+ for repr_traceback_data , repr_crash_data , description in reportdict [
463+ "longrepr"
464+ ]["chain" ]:
465+ chain .append (
466+ (
467+ deserialize_repr_traceback (repr_traceback_data ),
468+ deserialize_repr_crash (repr_crash_data ),
469+ description ,
470+ )
471+ )
472+ exception_info = ExceptionChainRepr (chain )
473+ else :
474+ exception_info = ReprExceptionInfo (reprtraceback , reprcrash )
448475
449476 for section in reportdict ["longrepr" ]["sections" ]:
450477 exception_info .addsection (* section )
0 commit comments