Python Glossary Русский

all Python 3doctestfunction

doctest.testsource()

debugging

Turns the examples from a module object's docstring into a plain Python script: code stays code and the expected output becomes comments.

Syntax

doctest.testsource(module, name)

The object is looked up by its full dotted name including the module — '__main__.f', not just 'f'; otherwise you get ValueError. The prose around the examples also ends up in the script as comments. Useful for stepping through a failing example in a debugger or porting a doctest to unittest.

Examples

import doctest, sys
def add(a, b):
    """
    >>> add(2, 3)
    5
    """
    return a + b
print(doctest.testsource(sys.modules["__main__"], "__main__.add"))
# → add(2, 3) / # Expected: / ## 5

See also

Open in the full glossarydocs.python.orgReport an inaccuracy