all Python 3doctestterm
doctest.DocTestParser
building blocks
Parses text containing >>> examples into code and expected output: get_examples() returns Example objects, get_doctest() a ready DocTest.
Syntax
doctest.DocTestParser().get_examples(string, name='<string>')
Expected output is everything after a >>> or ... line up to a blank line or the next >>>. Continuation lines start with ..., and the example indent must be consistent, otherwise ValueError. # doctest: directives are parsed here too and end up in Example.options.
Examples
import doctest
text = """
>>> total = 0
>>> for i in range(3):
... total += i
>>> total
3
>>> print('x') # doctest: +SKIP
x
"""
examples = doctest.DocTestParser().get_examples(text)
print(len(examples), repr(examples[2].want)) # → 4 '3\n'
print(examples[3].options == {doctest.SKIP: True}) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy