Python Glossary Русский

all Python 3doctestfunction

doctest.DocTestSuite()

unittest integration

Collects a module's doctests into a unittest.TestSuite so they run alongside regular tests through unittest or pytest.

Syntax

doctest.DocTestSuite(module=None, globs=None, extraglobs=None, test_finder=None, setUp=None, tearDown=None, optionflags=0)

Each docstring becomes one test case, so testsRun counts docstrings, not individual >>> lines. Without an argument it uses the calling module. A module with no doctests yields an empty suite instead of the ValueError raised before 3.5, so a wrong module path can go unnoticed. Hook it up through the load_tests protocol.

Examples

import doctest, io, unittest
def inc(x):
    """
    >>> inc(1)
    2
    >>> inc(-1)
    0
    """
    return x + 1
suite = doctest.DocTestSuite()
result = unittest.TextTestRunner(stream=io.StringIO()).run(suite)
print(result.testsRun, result.wasSuccessful())  # → 1 True — один докстринг, два примера

See also

Open in the full glossarydocs.python.orgReport an inaccuracy