Automated testing with doctest
doctest is a module in the Python standard library which
makes innovative use of Python's docstring, interactive shell, and
introspection capabilities.
docstrings
Python's docstrings provide an easy way to document modules, classes, methods, and functions. A docstring is simply a string constant that occurs as the first statement of an object's definition. Here is an example from the chapter on trees illustrating its use:
def total(tree): """total(tree) -> sum Return the sum of the values of the elements of a tree of numbers. """ if tree == None: return 0 return total(tree.left) + total(tree.right)
Assuming the example above is in a module named trees.py,
the following is now possible:
>>> from trees import *
>>> print total.__doc__
total(tree) -> sum
Return the sum of the values of the elements of a tree of numbers.