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

Function _report_to_json

src/_pytest/reports.py:447–517  ·  view source on GitHub ↗

Return the contents of this report as a dict of builtin entries, suitable for serialization. This was originally the serialize_report() function from xdist (ca03269).

(report: BaseReport)

Source from the content-addressed store, hash-verified

445
446
447def _report_to_json(report: BaseReport) -> Dict[str, Any]:
448 """Return the contents of this report as a dict of builtin entries,
449 suitable for serialization.
450
451 This was originally the serialize_report() function from xdist (ca03269).
452 """
453
454 def serialize_repr_entry(
455 entry: Union[ReprEntry, ReprEntryNative]
456 ) -> Dict[str, Any]:
457 data = attr.asdict(entry)
458 for key, value in data.items():
459 if hasattr(value, "__dict__"):
460 data[key] = attr.asdict(value)
461 entry_data = {"type": type(entry).__name__, "data": data}
462 return entry_data
463
464 def serialize_repr_traceback(reprtraceback: ReprTraceback) -> Dict[str, Any]:
465 result = attr.asdict(reprtraceback)
466 result["reprentries"] = [
467 serialize_repr_entry(x) for x in reprtraceback.reprentries
468 ]
469 return result
470
471 def serialize_repr_crash(
472 reprcrash: Optional[ReprFileLocation],
473 ) -> Optional[Dict[str, Any]]:
474 if reprcrash is not None:
475 return attr.asdict(reprcrash)
476 else:
477 return None
478
479 def serialize_exception_longrepr(rep: BaseReport) -> Dict[str, Any]:
480 assert rep.longrepr is not None
481 # TODO: Investigate whether the duck typing is really necessary here.
482 longrepr = cast(ExceptionRepr, rep.longrepr)
483 result: Dict[str, Any] = {
484 "reprcrash": serialize_repr_crash(longrepr.reprcrash),
485 "reprtraceback": serialize_repr_traceback(longrepr.reprtraceback),
486 "sections": longrepr.sections,
487 }
488 if isinstance(longrepr, ExceptionChainRepr):
489 result["chain"] = []
490 for repr_traceback, repr_crash, description in longrepr.chain:
491 result["chain"].append(
492 (
493 serialize_repr_traceback(repr_traceback),
494 serialize_repr_crash(repr_crash),
495 description,
496 )
497 )
498 else:
499 result["chain"] = None
500 return result
501
502 d = report.__dict__.copy()
503 if hasattr(report.longrepr, "toterminal"):
504 if hasattr(report.longrepr, "reprtraceback") and hasattr(

Callers 1

_to_jsonMethod · 0.85

Calls 2

fspathMethod · 0.80

Tested by

no test coverage detected