Navigation

The doctest module searches for pieces of text that look like interactive
Python sessions, and then executes those sessions to verify that they work
exactly as shown. There are several common ways to use doctest:

To check that a module’s docstrings are up-to-date by verifying that all
interactive examples still work as documented.

To perform regression testing by verifying that interactive examples from a
test file or a test object work as expected.

To write tutorial documentation for a package, liberally illustrated with
input-output examples. Depending on whether the examples or the expository text
are emphasized, this has the flavor of “literate testing” or “executable
documentation”.

That’s all you need to know to start making productive use of doctest!
Jump in. The following sections provide full details. Note that there are many
examples of doctests in the standard Python test suite and libraries.
Especially useful examples can be found in the standard test file
Lib/test/test_doctest.py.

The simplest way to start using doctest (but not necessarily the way you’ll
continue to do it) is to end each module M with:

if__name__=="__main__":importdoctestdoctest.testmod()

doctest then examines docstrings in module M.

Running the module as a script causes the examples in the docstrings to get
executed and verified:

pythonM.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, and the
final line of output is ***TestFailed***Nfailures., where N is the
number of examples that failed.

Run it with the -v switch instead:

pythonM.py-v

and a detailed report of all examples tried is printed to standard output, 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() (so passing -v or not
has no effect).

There is also a command line shortcut for running testmod(). You can
instruct the Python interpreter to run the doctest module directly from the
standard library and pass the module name(s) on the command line:

python-mdoctest-vexample.py

This will import example.py as a standalone module and run
testmod() on it. Note that this may not work correctly if the file is
part of a package and imports other submodules from that package.

Another simple application of doctest is testing interactive examples in a text
file. This can be done with the testfile() function:

importdoctestdoctest.testfile("example.txt")

That short script executes and verifies any interactive Python examples
contained in the file example.txt. The file content is treated as if it
were a single giant docstring; the file doesn’t need to contain a Python
program! For example, perhaps example.txt contains this:

The ``example`` module
======================
Using ``factorial``
-------------------
This is an example text file in reStructuredText format. First import
``factorial`` from the ``example`` module:
>>> from example import factorial
Now use it:
>>> factorial(6)
120

Running doctest.testfile("example.txt") then finds the error in this
documentation:

As with testmod(), testfile() won’t display anything unless an
example fails. If an example does fail, then the failing example(s) and the
cause(s) of the failure(s) are printed to stdout, using the same format as
testmod().

By default, testfile() looks for files in the calling module’s directory.
See section Basic API for a description of the optional arguments
that can be used to tell it to look for files in other locations.

Like testmod(), testfile()‘s verbosity can be set with the
-v command-line switch or with the optional keyword argument
verbose.

There is also a command line shortcut for running testfile(). You can
instruct the Python interpreter to run the doctest module directly from the
standard library and pass the file name(s) on the command line:

python-mdoctest-vexample.txt

Because the file name does not end with .py, doctest infers that
it must be run with testfile(), not testmod().

This section examines in detail how doctest works: which docstrings it looks at,
how it finds interactive examples, what execution context it uses, how it
handles exceptions, and how option flags can be used to control its behavior.
This is the information that you need to know to write doctest examples; for
information about actually running doctest on these examples, see the following
sections.

The module docstring, and all function, class and method docstrings are
searched. Objects imported into the module are not searched.

In addition, if M.__test__ exists and “is true”, it must be a dict, and each
entry maps a (string) name to a function object, class object, or string.
Function and class object docstrings found from M.__test__ are searched, and
strings are treated as if they were docstrings. In output, a key K in
M.__test__ appears with name

<nameofM>.__test__.K

Any classes found are recursively searched similarly, to test docstrings in
their contained methods and nested classes.

Any expected output must immediately follow the final '>>>' or '...'
line containing the code, and the expected output (if any) extends to the next
'>>>' or all-whitespace line.

The fine print:

Expected output cannot contain an all-whitespace line, since such a line is
taken to signal the end of expected output. If expected output does contain a
blank line, put <BLANKLINE> in your doctest example each place a blank line
is expected.

All hard tab characters are expanded to spaces, using 8-column tab stops.
Tabs in output generated by the tested code are not modified. Because any
hard tabs in the sample output are expanded, this means that if the code
output includes hard tabs, the only way the doctest can pass is if the
NORMALIZE_WHITESPACE option or directive is in effect.
Alternatively, the test can be rewritten to capture the output and compare it
to an expected value as part of the test. This handling of tabs in the
source was arrived at through trial and error, and has proven to be the least
error prone way of handling them. It is possible to use a different
algorithm for handling tabs by writing a custom DocTestParser class.

Output to stdout is captured, but not output to stderr (exception tracebacks
are captured via a different means).

If you continue a line via backslashing in an interactive session, or for any
other reason use a backslash, you should use a raw docstring, which will
preserve your backslashes exactly as you type them:

Otherwise, the backslash will be interpreted as part of the string. For example,
the “\” above would be interpreted as a newline character. Alternatively, you
can double each backslash in the doctest version (and not use a raw string):

By default, each time doctest finds a docstring to test, it uses a
shallow copy of M‘s globals, so that running tests doesn’t change the
module’s real globals, and so that one test in M can’t leave behind
crumbs that accidentally allow another test to work. This means examples can
freely use any names defined at top-level in M, and names defined earlier
in the docstring being run. Examples cannot see names defined in other
docstrings.

You can force use of your own dict as the execution context by passing
globs=your_dict to testmod() or testfile() instead.

No problem, provided that the traceback is the only output produced by the
example: just paste in the traceback. [1] Since tracebacks contain details
that are likely to change rapidly (for example, exact file paths and line
numbers), this is one case where doctest works hard to be flexible in what it
accepts.

That doctest succeeds if ValueError is raised, with the list.remove(x):xnotinlist detail as shown.

The expected output for an exception must start with a traceback header, which
may be either of the following two lines, indented the same as the first line of
the example:

Traceback(mostrecentcalllast):Traceback(innermostlast):

The traceback header is followed by an optional traceback stack, whose contents
are ignored by doctest. The traceback stack is typically omitted, or copied
verbatim from an interactive session.

The traceback stack is followed by the most interesting part: the line(s)
containing the exception type and detail. This is usually the last line of a
traceback, but can extend across multiple lines if the exception has a
multi-line detail:

Note that tracebacks are treated very specially. In particular, in the
rewritten example, the use of ... is independent of doctest’s
ELLIPSIS option. The ellipsis in that example could be left out, or
could just as well be three (or three hundred) commas or digits, or an indented
transcript of a Monty Python skit.

Some details you should read once, but won’t need to remember:

Doctest can’t guess whether your expected output came from an exception
traceback or from ordinary printing. So, e.g., an example that expects
ValueError:42isprime will pass whether ValueError is actually
raised or if the example merely prints that traceback text. In practice,
ordinary output rarely begins with a traceback header line, so this doesn’t
create real problems.

Each line of the traceback stack (if present) must be indented further than
the first line of the example, or start with a non-alphanumeric character.
The first line following the traceback header indented the same and starting
with an alphanumeric is taken to be the start of the exception detail. Of
course this does the right thing for genuine tracebacks.

When the IGNORE_EXCEPTION_DETAIL doctest option is is specified,
everything following the leftmost colon is ignored.

The interactive shell omits the traceback header line for some
SyntaxErrors. But doctest uses the traceback header line to
distinguish exceptions from non-exceptions. So in the rare case where you need
to test a SyntaxError that omits the traceback header, you will need to
manually add the traceback header line to your test example.

For some SyntaxErrors, Python displays the character position of the
syntax error, using a ^ marker:

>>> 1 1
File "<stdin>", line 1
1 1
^
SyntaxError: invalid syntax

Since the lines showing the position of the error come before the exception type
and detail, they are not checked by doctest. For example, the following test
would pass, even though it puts the ^ marker in the wrong location:

A number of option flags control various aspects of doctest’s behavior.
Symbolic names for the flags are supplied as module constants, which can be
or’ed together and passed to various functions. The names can also be used in
doctest directives (see below).

The first group of options define test semantics, controlling aspects of how
doctest decides whether actual output matches an example’s expected output:

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. When DONT_ACCEPT_TRUE_FOR_1 is
specified, neither substitution is allowed. The default behavior caters to that
Python changed the return type of many functions from integer to boolean;
doctests expecting “little integer” output still work in these cases. This
option will probably go away, but not for several years.

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. Because a genuinely blank line delimits the expected output, this is
the only way to communicate that a blank line is expected. When
DONT_ACCEPT_BLANKLINE is specified, this substitution is not allowed.

When specified, all sequences of whitespace (blanks and newlines) are treated as
equal. Any sequence of whitespace within the expected output will match any
sequence of whitespace within the actual output. By default, whitespace must
match exactly. NORMALIZE_WHITESPACE is especially useful when a line of
expected output is very long, and you want to wrap it across multiple lines in
your source.

When specified, an ellipsis marker (...) in the expected output can match
any substring in the actual output. This includes substrings that span line
boundaries, and empty substrings, so it’s best to keep usage of this simple.
Complicated uses can lead to the same kinds of “oops, it matched too much!”
surprises that .* is prone to in regular expressions.

When specified, an example that expects an exception passes if an exception of
the expected type is raised, even if the exception detail does not match. For
example, an example expecting ValueError:42 will pass if the actual
exception raised is ValueError:3*14, but will fail, e.g., if
TypeError is raised.

Note that a similar effect can be obtained using ELLIPSIS, and
IGNORE_EXCEPTION_DETAIL may go away when Python releases prior to 2.4
become uninteresting. Until then, IGNORE_EXCEPTION_DETAIL is the only
clear way to write a doctest that doesn’t care about the exception detail yet
continues to pass under Python releases prior to 2.4 (doctest directives appear
to be comments to them). For example,

When specified, do not run the example at all. This can be useful in contexts
where doctest examples serve as both documentation and test cases, and an
example should be included for documentation purposes, but should not be
checked. E.g., the example’s output might be random; or the example might
depend on resources which would be unavailable to the test driver.

The SKIP flag can also be used for temporarily “commenting out” examples.

When specified, differences are computed by difflib.Differ, using the same
algorithm as the popular ndiff.py utility. This is the only method that
marks differences within lines as well as across lines. For example, if a line
of expected output contains digit 1 where actual output contains letter
l, a line is inserted with a caret marking the mismatching column positions.

When specified, display the first failing example in each doctest, but suppress
output for all remaining examples. This will prevent doctest from reporting
correct examples that break because of earlier failures; but it might also hide
incorrect examples that fail independently of the first failure. When
REPORT_ONLY_FIRST_FAILURE is specified, the remaining examples are
still run, and still count towards the total number of failures reported; only
the output is suppressed.

Without the directive it would fail, both because the actual output doesn’t have
two blanks before the single-digit list elements, and because the actual output
is on a single line. This test also passes, and also requires a directive to do
so:

>>> print(list(range(20)))# doctest: +ELLIPSIS[0, 1, ..., 18, 19]

Multiple directives can be used on a single physical line, separated by commas:

Note that since all options are disabled by default, and directives apply only
to the example they appear in, enabling options (via + in a directive) is
usually the only meaningful choice. However, option flags can also be passed to
functions that run doctests, establishing different defaults. In such cases,
disabling an option via - in a directive can be useful.

There’s also a way to register new option flag names, although this isn’t useful
unless you intend to extend doctest internals via subclassing:

doctest is serious about requiring exact matches in expected output. If
even a single character doesn’t match, the test fails. This will probably
surprise you a few times, as you learn exactly what Python does and doesn’t
guarantee about output. For example, when printing a dict, Python doesn’t
guarantee that the key-value pairs will be printed in any particular order, so a
test like

All arguments except filename are optional, and should be specified in keyword
form.

Test examples in the file named filename. Return (failure_count,test_count).

Optional argument module_relative specifies how the filename should be
interpreted:

If module_relative is True (the default), then filename specifies an
OS-independent module-relative path. By default, this path is relative to the
calling module’s directory; but if the package argument is specified, then it
is relative to that package. To ensure OS-independence, filename should use
/ characters to separate path segments, and may not be an absolute path
(i.e., it may not begin with /).

If module_relative is False, then filename specifies an OS-specific
path. The path may be absolute or relative; relative paths are resolved with
respect to the current working directory.

Optional argument name gives the name of the test; by default, or if None,
os.path.basename(filename) is used.

Optional argument package is a Python package or the name of a Python package
whose directory should be used as the base directory for a module-relative
filename. If no package is specified, then the calling module’s directory is
used as the base directory for module-relative filenames. It is an error to
specify package if module_relative is False.

Optional argument globs gives a dict to be used as the globals when executing
examples. A new shallow copy of this dict is created for the doctest, so its
examples start with a clean slate. By default, or if None, a new empty dict
is used.

Optional argument extraglobs gives a dict merged into the globals used to
execute examples. This works like dict.update(): if globs and
extraglobs have a common key, the associated value in extraglobs appears in
the combined dict. By default, or if None, no extra globals are used. This
is an advanced feature that allows parameterization of doctests. For example, a
doctest can be written for a base class, using a generic name for the class,
then reused to test any number of subclasses by passing an extraglobs dict
mapping the generic name to the subclass to be tested.

Optional argument verbose prints lots of stuff if true, and prints only
failures if false; by default, or if None, it’s true if and only if '-v'
is in sys.argv.

Optional argument report prints a summary at the end when true, else prints
nothing at the end. In verbose mode, the summary is detailed, else the summary
is very brief (in fact, empty if all tests passed).

Optional argument raise_on_error defaults to false. If true, an exception is
raised upon the first failure or unexpected exception in an example. This
allows failures to be post-mortem debugged. Default behavior is to continue
running examples.

Optional argument parser specifies a DocTestParser (or subclass) that
should be used to extract tests from the files. It defaults to a normal parser
(i.e., DocTestParser()).

Optional argument encoding specifies an encoding that should be used to
convert the file to unicode.

All arguments are optional, and all except for m should be specified in
keyword form.

Test examples in docstrings in functions and classes reachable from module m
(or module __main__ if m is not supplied or is None), starting with
m.__doc__.

Also test examples reachable from dict m.__test__, if it exists and is not
None. m.__test__ maps names (strings) to functions, classes and
strings; function and class docstrings are searched for examples; strings are
searched directly, as if they were docstrings.

Only docstrings attached to objects belonging to module m are searched.

Return (failure_count,test_count).

Optional argument name gives the name of the module; by default, or if
None, m.__name__ is used.

Optional argument exclude_empty defaults to false. If true, objects for which
no doctests are found are excluded from consideration. The default is a backward
compatibility hack, so that code still using doctest.master.summarize() in
conjunction with testmod() continues to get output for objects with no
tests. The exclude_empty argument to the newer DocTestFinder
constructor defaults to true.

Optional arguments extraglobs, verbose, report, optionflags,
raise_on_error, and globs are the same as for function testfile()
above, except that globs defaults to m.__dict__.

There’s also a function to run the doctests associated with a single object.
This function is provided for backward compatibility. There are no plans to
deprecate it, but it’s rarely useful:

Test examples associated with object f; for example, f may be a module,
function, or class object.

A shallow copy of dictionary argument globs is used for the execution context.

Optional argument name is used in failure messages, and defaults to
"NoName".

If optional argument verbose is true, output is generated even if there are no
failures. By default, output is generated only in case of an example failure.

Optional argument compileflags gives the set of flags that should be used by
the Python compiler when running the examples. By default, or if None,
flags are deduced corresponding to the set of future features found in globs.

As your collection of doctest’ed modules grows, you’ll want a way to run all
their doctests systematically. doctest provides two functions that can
be used to create unittest test suites from modules and text files
containing doctests. These test suites can then be run using unittest
test runners:

The returned unittest.TestSuite is to be run by the unittest framework
and runs the interactive examples in each file. If an example in any file
fails, then the synthesized unit test fails, and a failureException
exception is raised showing the name of the file containing the test and a
(sometimes approximate) line number.

Pass one or more paths (as strings) to text files to be examined.

Options may be provided as keyword arguments:

Optional argument module_relative specifies how the filenames in paths
should be interpreted:

If module_relative is True (the default), then each filename in
paths specifies an OS-independent module-relative path. By default, this
path is relative to the calling module’s directory; but if the package
argument is specified, then it is relative to that package. To ensure
OS-independence, each filename should use / characters to separate path
segments, and may not be an absolute path (i.e., it may not begin with
/).

If module_relative is False, then each filename in paths specifies
an OS-specific path. The path may be absolute or relative; relative paths
are resolved with respect to the current working directory.

Optional argument package is a Python package or the name of a Python
package whose directory should be used as the base directory for
module-relative filenames in paths. If no package is specified, then the
calling module’s directory is used as the base directory for module-relative
filenames. It is an error to specify package if module_relative is
False.

Optional argument setUp specifies a set-up function for the test suite.
This is called before running the tests in each file. The setUp function
will be passed a DocTest object. The setUp function can access the
test globals as the globs attribute of the test passed.

Optional argument tearDown specifies a tear-down function for the test
suite. This is called after running the tests in each file. The tearDown
function will be passed a DocTest object. The setUp function can
access the test globals as the globs attribute of the test passed.

Optional argument globs is a dictionary containing the initial global
variables for the tests. A new copy of this dictionary is created for each
test. By default, globs is a new empty dictionary.

The returned unittest.TestSuite is to be run by the unittest framework
and runs each doctest in the module. If any of the doctests fail, then the
synthesized unit test fails, and a failureException exception is raised
showing the name of the file containing the test and a (sometimes approximate)
line number.

Optional argument module provides the module to be tested. It can be a module
object or a (possibly dotted) module name. If not specified, the module calling
this function is used.

Optional argument globs is a dictionary containing the initial global
variables for the tests. A new copy of this dictionary is created for each
test. By default, globs is a new empty dictionary.

Optional argument extraglobs specifies an extra set of global variables, which
is merged into globs. By default, no extra globals are used.

Optional argument test_finder is the DocTestFinder object (or a
drop-in replacement) that is used to extract doctests from the module.

Optional arguments setUp, tearDown, and optionflags are the same as for
function DocFileSuite() above.

Under the covers, DocTestSuite() creates a unittest.TestSuite out
of doctest.DocTestCase instances, and DocTestCase is a
subclass of unittest.TestCase. DocTestCase isn’t documented
here (it’s an internal detail), but studying its code can answer questions about
the exact details of unittest integration.

So both ways of creating a unittest.TestSuite run instances of
DocTestCase. This is important for a subtle reason: when you run
doctest functions yourself, you can control the doctest options in
use directly, by passing option flags to doctest functions. However, if
you’re writing a unittest framework, unittest ultimately controls
when and how tests get run. The framework author typically wants to control
doctest reporting options (perhaps, e.g., specified by command line
options), but there’s no way to pass options through unittest to
doctest test runners.

For this reason, doctest also supports a notion of doctest
reporting flags specific to unittest support, via this function:

This is a module-global setting, and affects all future doctests run by module
unittest: the runTest() method of DocTestCase looks at
the option flags specified for the test case when the DocTestCase
instance was constructed. If no reporting flags were specified (which is the
typical and expected case), doctest‘s unittest reporting flags are
or’ed into the option flags, and the option flags so augmented are passed to the
DocTestRunner instance created to run the doctest. If any reporting
flags were specified when the DocTestCase instance was constructed,
doctest‘s unittest reporting flags are ignored.

The value of the unittest reporting flags in effect before the function
was called is returned by the function.

The basic API is a simple wrapper that’s intended to make doctest easy to use.
It is fairly flexible, and should meet most users’ needs; however, if you
require more fine-grained control over testing, or wish to extend doctest’s
capabilities, then you should use the advanced API.

The advanced API revolves around two container classes, which are used to store
the interactive examples extracted from doctest cases:

The namespace (aka globals) that the examples should be run in. This is a
dictionary mapping names to values. Any changes to the namespace made by the
examples (such as binding new variables) will be reflected in globs
after the test is run.

The expected output from running the example’s source code (either from
stdout, or a traceback in case of exception). want ends with a
newline unless no output is expected, in which case it’s an empty string. The
constructor adds a newline when necessary.

The exception message generated by the example, if the example is expected to
generate an exception; or None if it is not expected to generate an
exception. This exception message is compared against the return value of
traceback.format_exception_only(). exc_msg ends with a newline
unless it’s None. The constructor adds a newline if needed.

A dictionary mapping from option flags to True or False, which is used
to override default options for this example. Any option flags not contained
in this dictionary are left at their default value (as specified by the
DocTestRunner‘s optionflags). By default, no options are set.

A processing class used to extract the DocTests that are relevant to
a given object, from its docstring and the docstrings of its contained objects.
DocTests can currently be extracted from the following object types:
modules, functions, classes, methods, staticmethods, classmethods, and
properties.

The optional argument verbose can be used to display the objects searched by
the finder. It defaults to False (no output).

The optional argument parser specifies the DocTestParser object (or a
drop-in replacement) that is used to extract doctests from docstrings.

If the optional argument recurse is false, then DocTestFinder.find()
will only examine the given object, and not any contained objects.

If the optional argument exclude_empty is false, then
DocTestFinder.find() will include tests for objects with empty docstrings.

Return a list of the DocTests that are defined by obj‘s
docstring, or by any of its contained objects’ docstrings.

The optional argument name specifies the object’s name; this name will be
used to construct names for the returned DocTests. If name is
not specified, then obj.__name__ is used.

The optional parameter module is the module that contains the given object.
If the module is not specified or is None, then the test finder will attempt
to automatically determine the correct module. The object’s module is used:

As a default namespace, if globs is not specified.

To prevent the DocTestFinder from extracting DocTests from objects that are
imported from other modules. (Contained objects with modules other than
module are ignored.)

To find the name of the file containing the object.

To help find the line number of the object within its file.

If module is False, no attempt to find the module will be made. This is
obscure, of use mostly in testing doctest itself: if module is False, or
is None but cannot be found automatically, then all objects are considered
to belong to the (non-existent) module, so all contained objects will
(recursively) be searched for doctests.

The globals for each DocTest is formed by combining globs and
extraglobs (bindings in extraglobs override bindings in globs). A new
shallow copy of the globals dictionary is created for each DocTest.
If globs is not specified, then it defaults to the module’s __dict__, if
specified, or {} otherwise. If extraglobs is not specified, then it
defaults to {}.

Extract all doctest examples from the given string, and return them as a list
of Example objects. Line numbers are 0-based. The optional argument
name is a name identifying this string, and is only used for error messages.

Divide the given string into examples and intervening text, and return them as
a list of alternating Examples and strings. Line numbers for the
Examples are 0-based. The optional argument name is a name
identifying this string, and is only used for error messages.

A processing class used to execute and verify the interactive examples in a
DocTest.

The comparison between expected outputs and actual outputs is done by an
OutputChecker. This comparison may be customized with a number of
option flags; see section Option Flags and Directives for more information. If the
option flags are insufficient, then the comparison may also be customized by
passing a subclass of OutputChecker to the constructor.

The test runner’s display output can be controlled in two ways. First, an output
function can be passed to TestRunner.run(); this function will be called
with strings that should be displayed. It defaults to sys.stdout.write. If
capturing the output is not sufficient, then the display output can be also
customized by subclassing DocTestRunner, and overriding the methods
report_start(), report_success(),
report_unexpected_exception(), and report_failure().

The optional keyword argument checker specifies the OutputChecker
object (or drop-in replacement) that should be used to compare the expected
outputs to the actual outputs of doctest examples.

The optional keyword argument verbose controls the DocTestRunner‘s
verbosity. If verbose is True, then information is printed about each
example, as it is run. If verbose is False, then only failures are
printed. If verbose is unspecified, or None, then verbose output is used
iff the command-line switch -v is used.

The optional keyword argument optionflags can be used to control how the test
runner compares expected output to actual output, and how it displays failures.
For more information, see section Option Flags and Directives.

Report that the given example raised an unexpected exception. This method is
provided to allow subclasses of DocTestRunner to customize their
output; it should not be called directly.

example is the example about to be processed. exc_info is a tuple
containing information about the unexpected exception (as returned by
sys.exc_info()). test is the test containing example. out is the
output function that was passed to DocTestRunner.run().

Run the examples in test (a DocTest object), and display the
results using the writer function out.

The examples are run in the namespace test.globs. If clear_globs is
true (the default), then this namespace will be cleared after the test runs,
to help with garbage collection. If you would like to examine the namespace
after the test completes, then use clear_globs=False.

compileflags gives the set of flags that should be used by the Python
compiler when running the examples. If not specified, then it will default to
the set of future-import flags that apply to globs.

The output of each example is checked using the DocTestRunner‘s
output checker, and the results are formatted by the
DocTestRunner.report_*() methods.

A class used to check the whether the actual output from a doctest example
matches the expected output. OutputChecker defines two methods:
check_output(), which compares a given pair of outputs, and returns true
if they match; and output_difference(), which returns a string describing
the differences between two outputs.

Return True iff the actual output from an example (got) matches the
expected output (want). These strings are always considered to match if
they are identical; but depending on what option flags the test runner is
using, several non-exact match types are also possible. See section
Option Flags and Directives for more information about option flags.

Several functions convert doctests to executable Python programs, which can be
run under the Python debugger, pdb.

The DebugRunner class is a subclass of DocTestRunner that
raises an exception for the first failing example, containing information about
that example. This information can be used to perform post-mortem debugging on
the example.

You can add a call to pdb.set_trace() in a doctest example, and you’ll
drop into the Python debugger when that line is executed. Then you can inspect
current values of variables, and so on. For example, suppose a.py
contains just this module docstring:

Argument s is a string containing doctest examples. The string is converted
to a Python script, where doctest examples in s are converted to regular code,
and everything else is converted to Python comments. The generated script is
returned as a string. For example,

Argument module is a module object, or dotted name of a module, containing the
object whose doctests are of interest. Argument name is the name (within the
module) of the object with the doctests of interest. The result is a string,
containing the object’s docstring converted to a Python script, as described for
script_from_examples() above. For example, if module a.py
contains a top-level function f(), then

importa,doctestprint(doctest.testsource(a,"a.f"))

prints a script version of function f()‘s docstring, with doctests
converted to code, and the rest placed in comments.

The module and name arguments are the same as for function
testsource() above. The synthesized Python script for the named object’s
docstring is written to a temporary file, and then that file is run under the
control of the Python debugger, pdb.

A shallow copy of module.__dict__ is used for both local and global
execution context.

Optional argument pm controls whether post-mortem debugging is used. If pm
has a true value, the script file is run directly, and the debugger gets
involved only if the script terminates via raising an unhandled exception. If
it does, then post-mortem debugging is invoked, via pdb.post_mortem(),
passing the traceback object from the unhandled exception. If pm is not
specified, or is false, the script is run under the debugger from the start, via
passing an appropriate exec() call to pdb.run().

This is like function debug() above, except that a string containing
doctest examples is specified directly, via the src argument.

Optional argument pm has the same meaning as in function debug() above.

Optional argument globs gives a dictionary to use as both local and global
execution context. If not specified, or None, an empty dictionary is used.
If specified, a shallow copy of the dictionary is used.

The DebugRunner class, and the special exceptions it may raise, are of
most interest to testing framework authors, and will only be sketched here. See
the source code, and especially DebugRunner‘s docstring (which is a
doctest!) for more details:

A subclass of DocTestRunner that raises an exception as soon as a
failure is encountered. If an unexpected exception occurs, an
UnexpectedException exception is raised, containing the test, the
example, and the original exception. If the output doesn’t match, then a
DocTestFailure exception is raised, containing the test, the example, and
the actual output.

For information about the constructor parameters and methods, see the
documentation for DocTestRunner in section Advanced API.

An exception raised by DocTestRunner to signal that a doctest example’s
actual output did not match its expected output. The constructor arguments are
used to initialize the member variables of the same names.

As mentioned in the introduction, doctest has grown to have three primary
uses:

Checking examples in docstrings.

Regression testing.

Executable documentation / literate testing.

These uses have different requirements, and it is important to distinguish them.
In particular, filling your docstrings with obscure test cases makes for bad
documentation.

When writing a docstring, choose docstring examples with care. There’s an art to
this that needs to be learned—it may not be natural at first. Examples should
add genuine value to the documentation. A good example can often be worth many
words. If done with care, the examples will be invaluable for your users, and
will pay back the time it takes to collect them many times over as the years go
by and things change. I’m still amazed at how often one of my doctest
examples stops working after a “harmless” change.

Doctest also makes an excellent tool for regression testing, especially if you
don’t skimp on explanatory text. By interleaving prose and examples, it becomes
much easier to keep track of what’s actually being tested, and why. When a test
fails, good prose can make it much easier to figure out what the problem is, and
how it should be fixed. It’s true that you could write extensive comments in
code-based testing, but few programmers do. Many have found that using doctest
approaches instead leads to much clearer tests. Perhaps this is simply because
doctest makes writing prose a little easier than writing code, while writing
comments in code is a little harder. I think it goes deeper than just that:
the natural attitude when writing a doctest-based test is that you want to
explain the fine points of your software, and illustrate them with examples.
This in turn naturally leads to test files that start with the simplest
features, and logically progress to complications and edge cases. A coherent
narrative is the result, instead of a collection of isolated functions that test
isolated bits of functionality seemingly at random. It’s a different attitude,
and produces different results, blurring the distinction between testing and
explaining.

Regression testing is best confined to dedicated objects or files. There are
several options for organizing tests:

Write text files containing test cases as interactive examples, and test the
files using testfile() or DocFileSuite(). This is recommended,
although is easiest to do for new projects, designed from the start to use
doctest.

Define functions named _regrtest_topic that consist of single docstrings,
containing test cases for the named topics. These functions can be included in
the same file as the module, or separated out into a separate test file.