PyDocs

Development Tools

doctest

Module doctest -- a framework for running examples in docstrings.

In simplest use, end each module M to be tested with:

def _test():
    import doctest
    doctest.testmod()

if __name__ == "__main__":
    _test()

Then running the module as a script will cause the examples in the
docstrings to get executed and verified:

python M.py

This won't display anything unless an example fails, in which case the
failing example(s) and the cause(s) of the failure(s) are printed to stdout
(why not stderr? because stderr is a lame hack <0.2 wink>), and the final
line of output is "Test failed.".

Run it with the -v switch instead:

python M.py -v

and a detailed report of all examples tried is printed to stdout, along
with assorted summaries at the end.

You can force verbose mode by passing "verbose=True" to testmod, or prohibit
it by passing "verbose=False".  In either of those cases, sys.argv is not
examined by testmod.

There are a variety of other ways to run doctests, including integration
with the unittest framework, and support for running non-Python text
files containing doctests.  There are also many ways to override parts
of doctest's default behaviors.  See the Library Reference Manual for
details.
Symbols in doctest

c

COMPARISON_FLAGS
A bitmask or’ing together all the comparison flags above.

d

debug
Debug a single doctest docstring.
debug_src
Debug a single doctest docstring, in argument `src`
DebugRunner
Run doc tests but raise an exception as soon as there is a failure.
DocFileSuite
A unittest suite for one or more doctest files.
DocTest
A collection of doctest examples that should be run in a single namespace.
DocTestFailure
A DocTest example has failed in debugging mode.
DocTestFinder
A class used to extract the DocTests that are relevant to a given object, from its docstring and the docstrings of its contained objects.
DocTestParser
A class used to parse strings containing doctest examples.
DocTestRunner
A class used to run DocTest test cases, and accumulate statistics.
DocTestSuite
Convert doctest tests for a module to a unittest test suite.
DONT_ACCEPT_BLANKLINE
By default, if an expected output block contains a line containing only the string <BLANKLINE>, then that line will match a blank line in the actual output.
DONT_ACCEPT_TRUE_FOR_1
By default, if an expected output block contains just 1, an actual output block containing just 1 or just True is considered to be a match, and similarly for 0 versus False.

e

ELLIPSIS
When specified, an ellipsis marker (...) in the expected output can match any substring in the actual output.
Example
A single doctest example, consisting of source code and expected output.

f

FAIL_FAST
When specified, exit after the first failing example and don’t attempt to run the remaining examples.

i

IGNORE_EXCEPTION_DETAIL
When specified, doctests expecting exceptions pass so long as an exception of the expected type is raised, even if the details (message and fully qualified exception name) don’t match.

n

NORMALIZE_WHITESPACE
When specified, all sequences of whitespace (blanks and newlines) are treated as equal.

o

OutputChecker
A class used to check whether the actual output from a doctest example matches the expected output.

r

register_optionflag
Create a new option flag with a given name, and return the new flag’s integer value.
REPORT_CDIFF
When specified, failures that involve multi-line expected and actual outputs will be displayed using a context diff.
REPORT_NDIFF
When specified, differences are computed by difflib.Differ, using the same algorithm as the popular ndiff.py utility.
REPORT_ONLY_FIRST_FAILURE
When specified, display the first failing example in each doctest, but suppress output for all remaining examples.
REPORT_UDIFF
When specified, failures that involve multi-line expected and actual outputs are displayed using a unified diff.
REPORTING_FLAGS
A bitmask or’ing together all the reporting flags above.
run_docstring_examples
Test examples in the given object's docstring (`f`), using `globs` as globals.

s

script_from_examples
Extract script from text with examples.
set_unittest_reportflags
Sets the unittest option flags.
SKIP
When specified, do not run the example at all.

t

testfile
Test examples in the given file.
testmod
m=None, name=None, globs=None, verbose=None, report=True, optionflags=0, extraglobs=None, raise_on_error=False, exclude_empty=False
testsource
Extract the test sources from a doctest docstring as a script.

u

UnexpectedException
A DocTest example has encountered an unexpected exception