Testing and Diagnostics Modules

doctest

Runs examples written in Python interactive-shell style and compares observed output with the expected text. Useful for keeping simple documentation examples correct; exact textual output can be brittle when values contain timings, addresses or environment-specific details.

Formal Syntax & Structure

python syntaxpython
import doctest

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
import doctesttext = ">>> 2 + 3\n5\n"test = doctest.DocTestParser().get_doctest(text, {}, "addition", "demo", 0)result = doctest.DocTestRunner().run(test)print(result.failed, result.attempted)
Python Reference