PyDocs

Nav

Development Tools

DocTestRunner

doctest

class (source)
  • DocTestRunner(checker=None, verbose=None, optionflags=0)
A class used to run DocTest test cases, and accumulate statistics.
The `run` method is used to process a single DocTest case.  It
returns a TestResults instance.

    >>> save_colorize = _colorize.COLORIZE
    >>> _colorize.COLORIZE = False

    >>> tests = DocTestFinder().find(_TestClass)
    >>> runner = DocTestRunner(verbose=False)
    >>> tests.sort(key = lambda test: test.name)
    >>> for test in tests:
    ...     print(test.name, '->', runner.run(test))
    _TestClass -> TestResults(failed=0, attempted=2)
    _TestClass.__init__ -> TestResults(failed=0, attempted=2)
    _TestClass.get -> TestResults(failed=0, attempted=2)
    _TestClass.square -> TestResults(failed=0, attempted=1)

The `summarize` method prints a summary of all the test cases that
have been run by the runner, and returns an aggregated TestResults
instance:

    >>> runner.summarize(verbose=1)
    4 items passed all tests:
       2 tests in _TestClass
       2 tests in _TestClass.__init__
       2 tests in _TestClass.get
       1 test in _TestClass.square
    7 tests in 4 items.
    7 passed.
    Test passed.
    TestResults(failed=0, attempted=7)

The aggregated number of tried examples and failed examples is also
available via the `tries`, `failures` and `skips` attributes:

    >>> runner.tries
    7
    >>> runner.failures
    0
    >>> runner.skips
    0

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 the documentation for `testmod` 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 (`out`) 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`.

    >>> _colorize.COLORIZE = save_colorize
Members of DocTestRunner

f

failures
Number of failed examples.

r

report_failure
Report that the given example failed.
report_skip
Report that the given example was skipped.
report_start
Report that the test runner is about to process the given example.
report_success
Report that the given example ran successfully.
report_unexpected_exception
Report that the given example raised an unexpected exception.
run
Run the examples in `test`, and display the results using the writer function `out`.

s

skips
Number of skipped examples.
summarize
Print a summary of all the test cases that have been run by this DocTestRunner, and return a TestResults instance.

t

tries
Number of attempted examples.
0 Examples
No examples for doctest/DocTestRunner.
See Also
No see-alsos for doctest/DocTestRunner
0 Notes
No notes for DocTestRunner