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.

Functions

DocFileSuite(*paths, **kw) A unittest suite for one or more doctest files.
DocFileTest(path[, module_relative, ...])
DocTestSuite([module, globs, extraglobs, ...]) Convert doctest tests for a module to a unittest test suite.
debug(module, name[, pm]) Debug a single doctest docstring.
debug_script(src[, pm, globs]) Debug a test script.
debug_src(src[, pm, globs]) Debug a single doctest docstring, in argument src
namedtuple(typename, field_names[, verbose, ...]) Returns a new subclass of tuple with named fields.
register_optionflag(name)
run_docstring_examples(f, globs[, verbose, ...]) Test examples in the given object’s docstring (f), using globs as globals.
script_from_examples(s) Extract script from text with examples.
set_unittest_reportflags(flags) Sets the unittest option flags.
testfile(filename[, module_relative, name, ...]) Test examples in the given file.
testmod([m, name, globs, verbose, report, ...]) m=None, name=None, globs=None, verbose=None, report=True,
testsource(module, name) Extract the test sources from a doctest docstring as a script.

Classes

DebugRunner([checker, verbose, optionflags]) Run doc tests but raise an exception as soon as there is a failure.
DocFileCase(test[, optionflags, setUp, ...])
DocTest(examples, globs, name, filename, ...) A collection of doctest examples that should be run in a single namespace.
DocTestCase(test[, optionflags, setUp, ...])
DocTestFinder([verbose, parser, recurse, ...]) 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([checker, verbose, optionflags]) A class used to run DocTest test cases, and accumulate statistics.
Example(source, want[, exc_msg, lineno, ...]) A single doctest example, consisting of source code and expected output.
OutputChecker A class used to check the whether the actual output from a doctest example matches the expected output.
SkipDocTestCase(module)
StringIO([buf]) class StringIO([buffer])
TestResults
Tester([mod, globs, verbose, optionflags])

Exceptions

DocTestFailure(test, example, got) A DocTest example has failed in debugging mode.
UnexpectedException(test, example, exc_info) A DocTest example has encountered an unexpected exception