The Python Book
 
doctest
20150227

Run a doctest on your python src file

.. first include unit tests in your docstrings:

eg. the file 'mydoctest.py'

#!/usr/bin/python3

def fact(n):
    '''
    Factorial.
    >>> fact(6)
    720
    >>> fact(7)
    5040
    '''
    return n*fact(n-1) if n>1 else n

Run the doctests:

python3 -m doctest mydoctest.py

Or from within python:

>>> import doctest
>>> doctest.testfile("mydoctest.py")

(hmm, doesn't work the way it should... import missing?)

 
Notes by Willem Moors. Generated on momo:/home/willem/sync/20151223_datamungingninja/pythonbook at 2019-07-31 19:22