Python Glossary Русский

all Python 3doctestterm

doctest.DocTestRunner

building blocks

Executes a DocTest and checks example output through an OutputChecker; run() returns TestResults and prints a report of failures.

Syntax

doctest.DocTestRunner(checker=None, verbose=None, optionflags=0).run(test, out=None)

The report goes to sys.stdout by default; redirect it with the out= argument of run(). After run() the test namespace is cleared (clear_globs=True), so variable values cannot be inspected afterwards. Comparison flags passed in optionflags apply to every example, and summarize() reports totals over all tests run.

Examples

import doctest
test = doctest.DocTestParser().get_doctest(">>> [1, 2, 3, 4]\n[1, ..., 4]\n", {}, "t", None, 0)
report = []
plain = doctest.DocTestRunner().run(test, out=report.append)
print(plain.failed, len(report) > 0)  # → 1 True — отчёт ушёл в список
test = doctest.DocTestParser().get_doctest(">>> [1, 2, 3, 4]\n[1, ..., 4]\n", {}, "t", None, 0)
print(doctest.DocTestRunner(optionflags=doctest.ELLIPSIS).run(test))  # → TestResults(failed=0, attempted=1)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy