5.2.1 Simple Usage: Checking Examples in Docstrings

Python PEP

5.2.1 Simple Usage: Checking Examples in Docstrings

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:

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

if __name__ == "__main__":
    _test()

doctest then examines docstrings in module M.

Running the module as a script causes 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, and the final line of output is "***Test Failed*** N failures.", where N is the number of examples that failed.

Run it with the -v switch instead:

python M.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).

For more information on testmod(), see section 5.2.4.

See About this document... for information on suggesting changes.