Time for action – executing doctests

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.

  1. The docstring will look like text you would see in a Python shell (including a prompt). We will rig one of the tests to fail, just to see what will happen.
      """
      Test for the factorial of 3 that should pass.
      >>> factorial(3)
      6
    
      Test for the factorial of 0 that should fail.
      >>> factorial(0)
      1
      """
  2. We will write the following line of NumPy code to compute the factorial:
    return np.arange(1, n+1).cumprod()[-1]

    We want this code to fail from time to time for demonstration purposes.

  3. We can run the 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

What just happened?

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]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.208.117