Python Glossary Русский

all Python 3doctestterm

doctest

module

Testing docstring examples: the module finds ">>>" sessions in text, runs them and compares the output with what is written.

Syntax

import doctest

doctest is good for keeping documentation examples from going stale: the example is the test. For a real test suite with fixtures and exception checks, unittest or pytest is more convenient. The output is compared literally as text, so it breaks on set ordering, object addresses and stray spaces.

Examples

import doctest
def square(x):
    """
    >>> square(3)
    9
    """
    return x * x
print(doctest.testmod().failed)  # → 0

See also

Open in the full glossarydocs.python.orgReport an inaccuracy