Let's write a simple example that is supposed to calculate the well-known factorial, but doesn't cover all the possible boundary conditions. In other words some tests will fail.
""" Test for the factorial of 3 that should pass. >>> factorial(3) 6 Test for the factorial of 0 that should fail. >>> factorial(0) 1 """
return np.arange(1, n+1).cumprod()[-1]
We want this code to fail from time to time for demonstration purposes.
doctest
by calling the rundocs
function of the numpy.testing
module for instance in the Python shell.>>>from numpy.testing import rundocs >>>rundocs('docstringtest.py') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "…/numpy/testing/utils.py", line 998, in rundocs raiseAssertionError("Some doctests failed: %s" % " ".join(msg)) AssertionError: Some doctests failed: ********************************************************************** File "docstringtest.py", line 10, in docstringtest.factorial Failed example: factorial(0) Exception raised: Traceback (most recent call last): File "…/doctest.py", line 1254, in __run compileflags, 1) in test.globs File "<doctestdocstringtest.factorial[1]>", line 1, in <module> factorial(0) File "docstringtest.py", line 13, in factorial return np.arange(1, n+1).cumprod()[-1] IndexError: index -1 is out of bounds for axis 0 with size 0
We wrote a docstring test which didn't take into account 0
and negative numbers. We run the test with the rundocs
function from the numpy.testing
module and got an index error as a result (see docstringtest.py
):
import numpy as np def factorial(n): """ Test for the factorial of 3 that should pass. >>> factorial(3) 6 Test for the factorial of 0 that should fail. >>> factorial(0) 1 """ return np.arange(1, n+1).cumprod()[-1]