{"record":{"id":"bc1a5c4def28c2b2","repo":"pytest-dev/pytest","slug":"multiple-doctest-failures","errorCode":null,"errorMessage":"Multiple doctest failures","messagePattern":"Multiple doctest failures","errorType":"exception","errorClass":"MultipleDoctestFailures","httpStatus":null,"severity":"error","filePath":"src/_pytest/doctest.py","lineNumber":304,"sourceCode":"        self.funcargs: dict[str, object] = {}\n        self._request = TopRequest(self, _ispytest=True)  # type: ignore[arg-type]\n\n    def setup(self) -> None:\n        self._request._fillfixtures()\n        globs = dict(getfixture=self._request.getfixturevalue)\n        for name, value in self._request.getfixturevalue(\"doctest_namespace\").items():\n            globs[name] = value\n        self.dtest.globs.update(globs)\n\n    def runtest(self) -> None:\n        _check_all_skipped(self.dtest)\n        self._disable_output_capturing_for_darwin()\n        failures: list[doctest.DocTestFailure] = []\n        # Type ignored because we change the type of `out` from what\n        # doctest expects.\n        self.runner.run(self.dtest, out=failures)  # type: ignore[arg-type]\n        if failures:\n            raise MultipleDoctestFailures(failures)\n\n    def _disable_output_capturing_for_darwin(self) -> None:\n        \"\"\"Disable output capturing. Otherwise, stdout is lost to doctest (#985).\"\"\"\n        if platform.system() != \"Darwin\":\n            return\n        capman = self.config.pluginmanager.getplugin(\"capturemanager\")\n        if capman:\n            capman.suspend_global_capture(in_=True)\n            out, err = capman.read_global_capture()\n            sys.stdout.write(out)\n            sys.stderr.write(err)\n\n    # TODO: Type ignored -- breaks Liskov Substitution.\n    def repr_failure(  # type: ignore[override]\n        self,\n        excinfo: ExceptionInfo[BaseException],\n    ) -> str | TerminalRepr:\n        import doctest","sourceCodeStart":286,"sourceCodeEnd":322,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/doctest.py#L286-L322","documentation":"When pytest runs a doctest example and the doctest module records one or more DocTestFailure entries (example output mismatch or unexpected exception), pytest bundles them into a MultipleDoctestFailures exception. The message is a static 'Multiple doctest failures' summary; the individual failures are attached as the exception's .failures list and shown in the traceback.","triggerScenarios":"A docstring example whose expected output differs from actual; an example that raises an exception not matching the expected; whitespace/precision drift in floating point output.","commonSituations":"Updating a library so repr/output changed; non-deterministic output (dict ordering, floats); copy-pasted examples not kept in sync with code; platform-specific line endings.","solutions":["Run pytest --doctest-modules on the specific file and read each listed failure to fix the expected output or the code.","Re-generate the expected output by copying the actual output once you have confirmed it is correct.","If the example is order-sensitive, sort collections or use # doctest: +NORMALIZE_WHITESPACE / +ELLIPSIS directives as appropriate."],"exampleFix":"// before\ndef add(a, b):\n    \"\"\"\n    >>> add(1, 2)\n    4\n    \"\"\"\n    return a + b\n// after\ndef add(a, b):\n    \"\"\"\n    >>> add(1, 2)\n    3\n    \"\"\"\n    return a + b","handlingStrategy":"try-catch","validationCode":"null","typeGuard":"null","tryCatchPattern":"from _pytest.doctest import MultipleDoctestFailures\n\ntry:\n    run_doctest()\nexcept MultipleDoctestFailures as e:\n    for f in e.failures:\n        print('example failed:', f.example, 'got:', f.got)\n    raise","preventionTips":["Run --doctest-modules in CI to catch drift early.","Use +ELLIPSIS and +NORMALIZE_WHITESPACE for non-deterministic output.","Avoid examples whose output depends on dict ordering or float precision."],"tags":["doctest","testing","documentation"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}