---
source: https://iinventedlight.com/library/notes/python-doctest.md
title: "Python doctest - executable examples in docstrings"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python docs, doctest - Test interactive Python examples; python/cpython (GitHub), Lib/doctest.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: doctest"
tags: [python, doctest, testing, docstrings, stdlib]
client: Librarian
sources:
  - id: S1
    title: "doctest - Test interactive Python examples"
    publisher: Python docs
    url: https://docs.python.org/3/library/doctest.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/doctest.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/doctest.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# Python doctest - executable examples in docstrings
**Answer:** doctest finds interactive examples in documentation and checks that they still work. A common use is to check that a module's docstrings are up-to-date. Call `doctest.testmod()` to run the examples in a module.

## Examples

```python
def factorial(n):
    """Return n!. For example:

    >>> factorial(5)
    120
    >>> factorial(0)
    1
    """
    return 1 if n <= 1 else n * factorial(n - 1)

if __name__ == "__main__":
    import doctest
    doctest.testmod()
```

```bash
python -m doctest -v mymodule.py
python -m doctest -v README.md          # examples in a text file
```

## Rules

- Examples start with a `>>>` prompt. Expected output follows until the next prompt or a blank line.
- `python -m doctest -v mymodule.py` runs the same checks from the command line.

## See also

- [python-unittest](https://iinventedlight.com/library/notes/python-unittest/)
- [python-inspect](https://iinventedlight.com/library/notes/python-inspect/)
- [python-ast](https://iinventedlight.com/library/notes/python-ast/)

## Claims

- id: C1
  claim: doctest can check that a module's docstrings are up to date.
  source: S1
  evidence: "To check that a module's docstrings are up-to-date"

- id: C2
  claim: testmod runs examples in a module.
  source: S2
  evidence: "doctest.testmod()"

- id: C3
  claim: Docstrings can hold the examples that doctest runs.
  source: S1
  evidence: "To check that a module's docstrings are up-to-date"

## Sources

- [S1] doctest - Test interactive Python examples. Python docs. https://docs.python.org/3/library/doctest.html . License: PSF-2.0. Accessed 2026-11-18.

- [S2] Lib/doctest.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/doctest.py . License: PSF-2.0. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, doctest - Test interactive Python examples; python/cpython (GitHub), Lib/doctest.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
