all Python 3doctestterm
doctest.DocTest
building blocks
The set of examples from one docstring or file together with the namespace they run in.
Syntax
doctest.DocTest(examples, globs, name, filename, lineno, docstring)
Each DocTest gets its own copy of globs, so a variable from one function's docstring is invisible in another's — a common source of NameError. Within one DocTest the examples, on the contrary, share state and depend on their order. Attributes: examples, globs, name, filename, lineno, docstring.
Examples
import doctest
parser = doctest.DocTestParser()
test = parser.get_doctest(">>> x = 5\n>>> x + 1\n6\n", {}, "demo", None, 0)
print(test.name, len(test.examples)) # → demo 2
print(doctest.DocTestRunner().run(test)) # → TestResults(failed=0, attempted=2)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy