Python doctest Module
Example
Run a simple doctest embedded in a docstring:
import doctest
def add(a, b):
"""
>>> add(2, 3)
5
"""
return a + b
r = doctest.testmod()
print(r.failed == 0)
Try it Yourself »
Definition and Usage
The doctest module searches for interactive Python examples in docstrings and verifies that they produce the expected results.
Use it to keep documentation examples in sync with code, add lightweight regression tests, and validate tutorials.
Members
Member | Description |
---|---|
blankline_marker | String used to indicate a blank line in expected output. |
debug() | Debug a single doctest. |
debug_src() | Debug a doctest given its source string. |
DocTest | Represents a single doctest, including source and expected output. |
DocTestFinder | Find doctests in modules and objects. |
DocTestParser | Parse text into a doctest. |
DocTestRunner | Run doctests and report results. |
OutputChecker | Compare expected with actual output and decide if they match. |
run_docstring_examples() | Run examples in the given docstring. |
testfile() | Run doctests in a text file. |
testmod() | Run doctests found in the calling module. |