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

Method repr_failure

src/_pytest/doctest.py:317–379  ·  view source on GitHub ↗
(  # type: ignore[override]
        self,
        excinfo: ExceptionInfo[BaseException],
    )

Source from the content-addressed store, hash-verified

315
316 # TODO: Type ignored -- breaks Liskov Substitution.
317 def repr_failure( # type: ignore[override]
318 self,
319 excinfo: ExceptionInfo[BaseException],
320 ) -> str | TerminalRepr:
321 import doctest
322
323 failures: (
324 Sequence[doctest.DocTestFailure | doctest.UnexpectedException] | None
325 ) = None
326 if isinstance(
327 excinfo.value, doctest.DocTestFailure | doctest.UnexpectedException
328 ):
329 failures = [excinfo.value]
330 elif isinstance(excinfo.value, MultipleDoctestFailures):
331 failures = excinfo.value.failures
332
333 if failures is None:
334 return super().repr_failure(excinfo)
335
336 reprlocation_lines = []
337 for failure in failures:
338 example = failure.example
339 test = failure.test
340 filename = test.filename
341 if test.lineno is None:
342 lineno = None
343 else:
344 lineno = test.lineno + example.lineno + 1
345 message = type(failure).__name__
346 # TODO: ReprFileLocation doesn't expect a None lineno.
347 reprlocation = ReprFileLocation(filename, lineno, message) # type: ignore[arg-type]
348 checker = _get_checker()
349 report_choice = _get_report_choice(self.config.getoption("doctestreport"))
350 if lineno is not None:
351 assert failure.test.docstring is not None
352 lines = failure.test.docstring.splitlines(False)
353 # add line numbers to the left of the error message
354 assert test.lineno is not None
355 lines = [
356 f"{i + test.lineno + 1:03d} {x}" for (i, x) in enumerate(lines)
357 ]
358 # trim docstring error lines to 10
359 lines = lines[max(example.lineno - 9, 0) : example.lineno + 1]
360 else:
361 lines = [
362 "EXAMPLE LOCATION UNKNOWN, not showing all tests of that example"
363 ]
364 indent = ">>>"
365 for line in example.source.splitlines():
366 lines.append(f"??? {indent} {line}")
367 indent = "..."
368 if isinstance(failure, doctest.DocTestFailure):
369 lines += checker.output_difference(
370 example, failure.got, report_choice
371 ).split("\n")
372 else:
373 inner_excinfo = ExceptionInfo.from_exc_info(failure.exc_info)
374 lines += [f"UNEXPECTED EXCEPTION: {inner_excinfo.value!r}"]

Callers

nothing calls this directly

Calls 8

ReprFileLocationClass · 0.90
_get_checkerFunction · 0.85
_get_report_choiceFunction · 0.85
ReprFailDoctestClass · 0.85
getoptionMethod · 0.80
appendMethod · 0.80
from_exc_infoMethod · 0.80
stripMethod · 0.80

Tested by

no test coverage detected