|
The Python Book
|
|
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?)
| |