Python Glossary Русский

all Python 3doctestterm

doctest.Example

building blocks

A single doctest example: the source after >>> and the expected output, plus line number, indent and directives.

Syntax

doctest.Example(source, want, exc_msg=None, lineno=0, indent=0, options=None)

source and want always end with a newline — want for no output is '', not '\n'. Expected output stops at a blank line, so a real blank line in the output is written as <BLANKLINE>. For an example that raises, exc_msg holds the message, and directives are stored in options as {flag: True}.

Examples

import doctest
ex = doctest.DocTestParser().get_examples(">>> print('a\\n\\nb')\na\n<BLANKLINE>\nb\n")[0]
print(repr(ex.source), repr(ex.want))  # → "print('a\\n\\nb')\n" 'a\n<BLANKLINE>\nb\n'
bad = doctest.DocTestParser().get_examples(">>> 1/0\nTraceback (most recent call last):\nZeroDivisionError: division by zero\n")[0]
print(repr(bad.exc_msg))  # → 'division by zero\n'

See also

Open in the full glossarydocs.python.orgReport an inaccuracy