5.2.1 Normal Usage

In normal use, end each module M with:

def _test():
    import doctest, M           # replace M with your module's name
    return doctest.testmod(M)   # ditto

if __name__ == "__main__":
    _test()

If you want to test the module as the main module, you don't need to pass M to testmod(); in this case, it will test the current module.

Then 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.'.

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=1 to testmod(), or prohibit it by passing verbose=0. In either of those cases, sys.argv is not examined by testmod().

In any case, testmod() returns a 2-tuple of ints (f, t), where f is the number of docstring examples that failed and t is the total number of docstring examples attempted.

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