# Run Sphinx doctests without mistaking them for a full test suite

Canonical: https://githtml.com/guides/sphinx-doctest-examples-local-check
Published: 2026-11-23
Publisher: WaveTech LLC / gitHtml

Use Sphinx's doctest builder to check examples deliberately marked for execution, with safe fixtures and explicit setup. Treat the results as evidence about those examples, not the entire application. Review code before running it, control external dependencies, and distinguish output normalization from changes that would hide a genuine behavior mismatch.

## Choose examples that can be executed responsibly

A short deterministic example is a good candidate for a documentation test. A command that deletes files, sends messages, or modifies a real service is not appropriate for casual execution during a documentation build. Inventory the examples and identify their side effects before enabling the builder. Use disposable local data and approved test services where necessary. State which examples are checked and which remain illustrative. This avoids a misleading impression that every code block in the documentation has been run. Keep prerequisites close to the example so readers and test maintainers share the same assumptions.

## Make expected observations meaningful

Enable the extension through the project's maintained configuration and start with a small example like the fixture below. Use stable inputs and outputs where possible. If timestamps, random identifiers, or platform details vary, decide which properties matter rather than blindly matching a copied transcript. Do not broadly ignore output just to make a failure disappear. A changed result may reveal stale documentation or a real implementation change. Separate setup and cleanup from the teaching example when the tool supports it, but keep their effects documented. Readers should understand the state required even if the testing harness prepares it invisibly.

````rst
.. doctest::

   >>> sum([2, 3, 5])
   10

# Run separately in the terminal:
# sphinx-build -b doctest docs docs/_build/doctest
````

## Interpret a passing run narrowly

Run the doctest builder locally and inspect both its result and any warnings. A pass means the selected examples matched the configured expectations in that environment. It does not establish production readiness, complete branch coverage, or the safety of untested alternatives. Review the rendered HTML as well, since a working example can still be presented without a required explanation or with misleading surrounding prose. Preserve the test fixtures and version assumptions so future changes can rerun the same checks. When an example changes, review its pedagogical purpose as well as the assertion; a technically passing replacement may no longer teach the intended behavior. Make failure reports identify the example and its setup context. A maintainer should not have to infer whether a mismatch came from the teaching code, a shared fixture, or an external dependency.

1. Review examples for side effects and choose safe deterministic fixtures.
2. Document setup that the test harness performs on the reader's behalf.
3. Investigate mismatches before relaxing output comparison.
4. Report which examples ran and keep the limits of that evidence explicit.

## Sources and further reading

- [Sphinx doctest](https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html)

## Related guides

- [Diagnose missing Sphinx intersphinx references](https://githtml.com/guides/sphinx-intersphinx-missing-reference)
- [Add MyST Markdown to Sphinx without assuming plain Markdown behavior](https://githtml.com/guides/sphinx-myst-markdown-authoring-boundary)
- [Triage Sphinx unresolved references with nitpicky builds](https://githtml.com/guides/sphinx-nitpicky-warning-triage)
- [Check an Astro Starlight documentation build locally](https://githtml.com/guides/starlight-local-build-content-validation)

Editorial approach: https://githtml.com/guides/about
