Python Glossary Русский

all Python 3doctestfunction

doctest.run_docstring_examples()

running tests

Checks the examples in a single object's docstring — a function or a class. Handy when you need one function rather than the whole module.

Syntax

doctest.run_docstring_examples(f, globs, verbose=False, name='NoName', compileflags=None, optionflags=0)

It returns nothing and stays silent on success — only failures are reported. The second argument, globs, is the namespace for the examples, and if the function itself is not in it (usually pass globals()), every example fails with NameError. The name argument sets the name used in the report.

Examples

import doctest
def greet(name):
    """
    >>> greet("Ann")
    'Hi, Ann'
    """
    return "Hi, " + name
print(doctest.run_docstring_examples(greet, globals(), name="greet"))  # → None — успех молчит
doctest.run_docstring_examples(greet, globals(), verbose=True, name="greet")
# → Trying: / greet("Ann") / Expecting: / 'Hi, Ann' / ok

See also

Open in the full glossarydocs.python.orgReport an inaccuracy