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

Method repr_failure

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 8

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

Tested by

no test coverage detected