Python Glossary Русский

all Python 3doctestterm

doctest.OutputChecker

building blocks

Compares an example's expected and actual output: check_output() decides whether they match, taking flags such as ELLIPSIS into account.

Syntax

doctest.OutputChecker().check_output(want, got, optionflags)

Comparison is textual, not by value: 3.0 vs 3, {1, 2} vs {2, 1} and different <... at 0x...> addresses all differ. There is one non-obvious exception: 1 is accepted where True is expected, and 0 for False, unless DONT_ACCEPT_TRUE_FOR_1 is set. ELLIPSIS allows ..., NORMALIZE_WHITESPACE tolerates spacing; a subclass can add its own rules.

Examples

import doctest
check = doctest.OutputChecker().check_output
print(check("3\n", "3.0\n", 0))  # → False — сравнивается текст
print(check("[1, ..., 5]\n", "[1, 2, 3, 4, 5]\n", doctest.ELLIPSIS))  # → True
print(check("a b\n", "a    b\n", doctest.NORMALIZE_WHITESPACE))  # → True
print(check("True\n", "1\n", 0), check("True\n", "1\n", doctest.DONT_ACCEPT_TRUE_FOR_1))  # → True False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy