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:
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
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
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
t), where f is the number of docstring examples that
failed and t is the total number of docstring examples
See About this document... for information on suggesting changes.