Module doctest
[hide private]
[frames] | no frames]

Module 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.

Classes [hide private]
_SpoofOut
_OutputRedirectingPdb
A specialized version of the python debugger that redirects stdout to a given stream when interacting with the user.
Example
A single doctest example, consisting of source code and expected output.
DocTest
A collection of doctest examples that should be run in a single namespace.
DocTestParser
A class used to parse strings containing doctest examples.
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.
DocTestRunner
A class used to run DocTest test cases, and accumulate statistics.
OutputChecker
A class used to check the whether the actual output from a doctest example matches the expected output.
DocTestFailure
A DocTest example has failed in debugging mode.
UnexpectedException
A DocTest example has encountered an unexpected exception
DebugRunner
Run doc tests but raise an exception as soon as there is a failure.
Tester
DocTestCase
DocFileCase
_TestClass
A pointless class, for sanity-checking of docstring testing.
Functions [hide private]
 
register_optionflag(name)
 
_extract_future_flags(globs)
Return the compiler-flags associated with the future features that have been imported into the given namespace (globs).
 
_normalize_module(module, depth=2)
If module is a module, then return module.
 
_load_testfile(filename, package, module_relative)
 
_indent(s, indent=4)
Add the given number of space characters to the beginning every non-blank line in s, and return the result.
 
_exception_traceback(exc_info)
Return a string containing a traceback message for the given exc_info tuple (as returned by sys.exc_info()).
 
_ellipsis_match(want, got)
Essentially the only subtle case: >>> _ellipsis_match('aa...aa', 'aaa') False
 
_comment_line(line)
Return a commented form of the given line
 
_module_relative_path(module, path)
 
testmod(m=None, name=None, globs=None, verbose=None, report=True, optionflags=0, extraglobs=None, raise_on_error=False, exclude_empty=False)
optionflags=0, extraglobs=None, raise_on_error=False, exclude_empty=False
 
testfile(filename, module_relative=True, name=None, package=None, globs=None, verbose=None, report=True, optionflags=0, extraglobs=None, raise_on_error=False, parser=DocTestParser(), encoding=None)
Test examples in the given file.
 
run_docstring_examples(f, globs, verbose=False, name='NoName', compileflags=None, optionflags=0)
Test examples in the given object's docstring (f), using globs as globals.
 
set_unittest_reportflags(flags)
Sets the unittest option flags.
 
DocTestSuite(module=None, globs=None, extraglobs=None, test_finder=None, **options)
Convert doctest tests for a module to a unittest test suite.
 
DocFileTest(path, module_relative=True, package=None, globs=None, parser=DocTestParser(), encoding=None, **options)
 
DocFileSuite(*paths, **kw)
A unittest suite for one or more doctest files.
 
script_from_examples(s)
Extract script from text with examples.
 
testsource(module, name)
Extract the test sources from a doctest docstring as a script.
 
debug_src(src, pm=False, globs=None)
Debug a single doctest docstring, in argument src'
 
debug_script(src, pm=False, globs=None)
Debug a test script.
 
debug(module, name, pm=False)
Debug a single doctest docstring.
 
_test()
Variables [hide private]
  OPTIONFLAGS_BY_NAME = {'DONT_ACCEPT_BLANKLINE': 2, 'DONT_ACCEP...
  DONT_ACCEPT_TRUE_FOR_1 = 1
  DONT_ACCEPT_BLANKLINE = 2
  NORMALIZE_WHITESPACE = 4
  ELLIPSIS = 8
  SKIP = 16
  IGNORE_EXCEPTION_DETAIL = 32
  COMPARISON_FLAGS = 63
  REPORT_UDIFF = 64
  REPORT_CDIFF = 128
  REPORT_NDIFF = 256
  REPORT_ONLY_FIRST_FAILURE = 512
  REPORTING_FLAGS = 960
  BLANKLINE_MARKER = '<BLANKLINE>'
  ELLIPSIS_MARKER = '...'
  master = None
  _unittest_reportflags = 0
  __test__ = {"_TestClass": _TestClass, "string": r...

Imports: __future__, sys, traceback, inspect, linecache, os, re, unittest, difflib, pdb, tempfile, warnings, StringIO


Function Details [hide private]

_normalize_module(module, depth=2)

 
Return the module specified by module. In particular:
  • If module is a module, then return module.
  • If module is a string, then import and return the module with that name.
  • If module is None, then return the calling module. The calling module is assumed to be the module of the stack frame at the given depth in the call stack.

testmod(m=None, name=None, globs=None, verbose=None, report=True, optionflags=0, extraglobs=None, raise_on_error=False, exclude_empty=False)

 
m=None, name=None, globs=None, verbose=None, report=True,
optionflags=0, extraglobs=None, raise_on_error=False, exclude_empty=False

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

Also test examples reachable from dict m.__test__ if it exists and is not None. m.__test__ maps names to functions, classes and strings; function and class docstrings are tested even if the name is private; strings are tested directly, as if they were docstrings.

Return (#failures, #tests).

See doctest.__doc__ for an overview.

Optional keyword arg "name" gives the name of the module; by default use m.__name__.

Optional keyword arg "globs" gives a dict to be used as the globals when executing examples; by default, use m.__dict__. A copy of this dict is actually used for each docstring, so that each docstring's examples start with a clean slate.

Optional keyword arg "extraglobs" gives a dictionary that should be merged into the globals that are used to execute examples. By default, no extra globals are used. This is new in 2.4.

Optional keyword arg "verbose" prints lots of stuff if true, prints only failures if false; by default, it's true iff "-v" is in sys.argv.

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

Optional keyword arg "optionflags" or's together module constants, and defaults to 0. This is new in 2.3. Possible values (see the docs for details):

DONT_ACCEPT_TRUE_FOR_1 DONT_ACCEPT_BLANKLINE NORMALIZE_WHITESPACE ELLIPSIS SKIP IGNORE_EXCEPTION_DETAIL REPORT_UDIFF REPORT_CDIFF REPORT_NDIFF REPORT_ONLY_FIRST_FAILURE

Optional keyword arg "raise_on_error" raises an exception on the first unexpected exception or failure. This allows failures to be post-mortem debugged.

Advanced tomfoolery: testmod runs methods of a local instance of class doctest.Tester, then merges the results into (or creates) global Tester instance doctest.master. Methods of doctest.master can be called directly too, if you want to do something unusual. Passing report=0 to testmod is especially useful then, to delay displaying a summary. Invoke doctest.master.summarize(verbose) when you're done fiddling.

testfile(filename, module_relative=True, name=None, package=None, globs=None, verbose=None, report=True, optionflags=0, extraglobs=None, raise_on_error=False, parser=DocTestParser(), encoding=None)

 

Test examples in the given file. Return (#failures, #tests).

Optional keyword arg "module_relative" specifies how filenames should be interpreted:

  • If "module_relative" is True (the default), then "filename"

    specifies a 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 should 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 (to the current working directory).

Optional keyword arg "name" gives the name of the test; by default use the file's basename.

Optional keyword 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 keyword arg "globs" gives a dict to be used as the globals when executing examples; by default, use {}. A copy of this dict is actually used for each docstring, so that each docstring's examples start with a clean slate.

Optional keyword arg "extraglobs" gives a dictionary that should be merged into the globals that are used to execute examples. By default, no extra globals are used.

Optional keyword arg "verbose" prints lots of stuff if true, prints only failures if false; by default, it's true iff "-v" is in sys.argv.

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

Optional keyword arg "optionflags" or's together module constants, and defaults to 0. Possible values (see the docs for details):

DONT_ACCEPT_TRUE_FOR_1 DONT_ACCEPT_BLANKLINE NORMALIZE_WHITESPACE ELLIPSIS SKIP IGNORE_EXCEPTION_DETAIL REPORT_UDIFF REPORT_CDIFF REPORT_NDIFF REPORT_ONLY_FIRST_FAILURE

Optional keyword arg "raise_on_error" raises an exception on the first unexpected exception or failure. This allows failures to be post-mortem debugged.

Optional keyword arg "parser" specifies a DocTestParser (or subclass) that should be used to extract tests from the files.

Optional keyword arg "encoding" specifies an encoding that should be used to convert the file to unicode.

Advanced tomfoolery: testmod runs methods of a local instance of class doctest.Tester, then merges the results into (or creates) global Tester instance doctest.master. Methods of doctest.master can be called directly too, if you want to do something unusual. Passing report=0 to testmod is especially useful then, to delay displaying a summary. Invoke doctest.master.summarize(verbose) when you're done fiddling.

run_docstring_examples(f, globs, verbose=False, name='NoName', compileflags=None, optionflags=0)

 

Test examples in the given object's docstring (f), using globs as globals. Optional argument name is used in failure messages. If the optional argument verbose is true, then generate output even if there are no failures.

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.

Optional keyword arg optionflags specifies options for the testing and output. See the documentation for testmod for more information.

set_unittest_reportflags(flags)

 

Sets the unittest option flags.

The old flag is returned so that a runner could restore the old value if it wished to:

>>> import doctest
>>> old = doctest._unittest_reportflags
>>> doctest.set_unittest_reportflags(REPORT_NDIFF |
...                          REPORT_ONLY_FIRST_FAILURE) == old
True
>>> doctest._unittest_reportflags == (REPORT_NDIFF |
...                                   REPORT_ONLY_FIRST_FAILURE)
True

Only reporting flags can be set:

>>> doctest.set_unittest_reportflags(ELLIPSIS)
Traceback (most recent call last):
...
ValueError: ('Only reporting flags allowed', 8)
>>> doctest.set_unittest_reportflags(old) == (REPORT_NDIFF |
...                                   REPORT_ONLY_FIRST_FAILURE)
True

DocTestSuite(module=None, globs=None, extraglobs=None, test_finder=None, **options)

 

Convert doctest tests for a module to a unittest test suite.

This converts each documentation string in a module that contains doctest tests to a unittest test case. If any of the tests in a doc string fail, then the test case fails. An exception is raised showing the name of the file containing the test and a (sometimes approximate) line number.

The module argument provides the module to be tested. The argument can be either a module or a module name.

If no argument is given, the calling module is used.

A number of options may be provided as keyword arguments:

setUp
A set-up function. 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.
tearDown
A tear-down function. This is called after running the tests in each file. The tearDown function will be passed a DocTest object. The tearDown function can access the test globals as the globs attribute of the test passed.
globs
A dictionary containing initial global variables for the tests.
optionflags
A set of doctest option flags expressed as an integer.

DocFileSuite(*paths, **kw)

 

A unittest suite for one or more doctest files.

The path to each doctest file is given as a string; the interpretation of that string depends on the keyword argument "module_relative".

A number of options may be provided as keyword arguments:

module_relative

If "module_relative" is True, then the given file paths are interpreted as os-independent module-relative paths. By default, these paths are relative to the calling module's directory; but if the "package" argument is specified, then they are 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 the given file paths are interpreted as os-specific paths. These paths may be absolute or relative (to the current working directory).

package
A Python package or the name of a Python package whose directory should be used as the base directory for module relative paths. If "package" is not 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.
setUp
A set-up function. 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.
tearDown
A tear-down function. This is called after running the tests in each file. The tearDown function will be passed a DocTest object. The tearDown function can access the test globals as the globs attribute of the test passed.
globs
A dictionary containing initial global variables for the tests.
optionflags
A set of doctest option flags expressed as an integer.
parser
A DocTestParser (or subclass) that should be used to extract tests from the files.
encoding
An encoding that will be used to convert the files to unicode.

script_from_examples(s)

 

Extract script from text with examples.

Converts text with examples to a Python script. Example input is converted to regular code. Example output and all other words are converted to comments:

>>> text = '''
...       Here are examples of simple math.
...
...           Python has super accurate integer addition
...
...           >>> 2 + 2
...           5
...
...           And very friendly error messages:
...
...           >>> 1/0
...           To Infinity
...           And
...           Beyond
...
...           You can use logic if you want:
...
...           >>> if 0:
...           ...    blah
...           ...    blah
...           ...
...
...           Ho hum
...           '''
>>> print script_from_examples(text)
# Here are examples of simple math.
#
#     Python has super accurate integer addition
#
2 + 2
# Expected:
## 5
#
#     And very friendly error messages:
#
1/0
# Expected:
## To Infinity
## And
## Beyond
#
#     You can use logic if you want:
#
if 0:
   blah
   blah
#
#     Ho hum
<BLANKLINE>

testsource(module, name)

 

Extract the test sources from a doctest docstring as a script.

Provide the module (or dotted name of the module) containing the test to be debugged and the name (within the module) of the object with the doc string with tests to be debugged.

debug(module, name, pm=False)

 

Debug a single doctest docstring.

Provide the module (or dotted name of the module) containing the test to be debugged and the name (within the module) of the object with the docstring with tests to be debugged.


Variables Details [hide private]

OPTIONFLAGS_BY_NAME

Value:
{'DONT_ACCEPT_BLANKLINE': 2,
 'DONT_ACCEPT_TRUE_FOR_1': 1,
 'ELLIPSIS': 8,
 'IGNORE_EXCEPTION_DETAIL': 32,
 'NORMALIZE_WHITESPACE': 4,
 'REPORT_CDIFF': 128,
 'REPORT_NDIFF': 256,
 'REPORT_ONLY_FIRST_FAILURE': 512,
...

__test__

Value:
{'_TestClass': <class doctest._TestClass at 0x4068e7dc>,
 'blank lines': '''
                Blank lines can be marked with <BLANKLINE>:
                    >>> print \'foo\\n\\nbar\\n\'
                    foo
                    <BLANKLINE>
                    bar
                    <BLANKLINE>
...