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

Function _report_to_json

src/_pytest/reports.py:541–611  ·  src/_pytest/reports.py::_report_to_json

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

539
540
541def _report_to_json(report: BaseReport) -> dict[str, Any]:
542 class="st">"""Return the contents of this report as a dict of builtin entries,
543 suitable for serialization.
544
545 This was originally the serialize_report() function from xdist (ca03269).
546 class="st">"""
547
548 def serialize_repr_entry(
549 entry: ReprEntry | ReprEntryNative,
550 ) -> dict[str, Any]:
551 data = dataclasses.asdict(entry)
552 for key, value in data.items():
553 if hasattr(value, class="st">"__dict__"):
554 data[key] = dataclasses.asdict(value)
555 entry_data = {class="st">"type": type(entry).__name__, class="st">"data": data}
556 return entry_data
557
558 def serialize_repr_traceback(reprtraceback: ReprTraceback) -> dict[str, Any]:
559 result = dataclasses.asdict(reprtraceback)
560 result[class="st">"reprentries"] = [
561 serialize_repr_entry(x) for x in reprtraceback.reprentries
562 ]
563 return result
564
565 def serialize_repr_crash(
566 reprcrash: ReprFileLocation | None,
567 ) -> dict[str, Any] | None:
568 if reprcrash is not None:
569 return dataclasses.asdict(reprcrash)
570 else:
571 return None
572
573 def serialize_exception_longrepr(rep: BaseReport) -> dict[str, Any]:
574 assert rep.longrepr is not None
575 class="cm"># TODO: Investigate whether the duck typing is really necessary here.
576 longrepr = cast(ExceptionRepr, rep.longrepr)
577 result: dict[str, Any] = {
578 class="st">"reprcrash": serialize_repr_crash(longrepr.reprcrash),
579 class="st">"reprtraceback": serialize_repr_traceback(longrepr.reprtraceback),
580 class="st">"sections": longrepr.sections,
581 }
582 if isinstance(longrepr, ExceptionChainRepr):
583 result[class="st">"chain"] = []
584 for repr_traceback, repr_crash, description in longrepr.chain:
585 result[class="st">"chain"].append(
586 (
587 serialize_repr_traceback(repr_traceback),
588 serialize_repr_crash(repr_crash),
589 description,
590 )
591 )
592 else:
593 result[class="st">"chain"] = None
594 return result
595
596 d = report.__dict__.copy()
597 if hasattr(report.longrepr, class="st">"toterminal"):
598 if hasattr(report.longrepr, class="st">"reprtraceback") and hasattr(

Callers 1

_to_jsonMethod · 0.85

Calls 3

copyMethod · 0.80
fspathMethod · 0.80

Tested by

no test coverage detected