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 of 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). 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. Write the following line of NumPy code:
    return np.arange(1, n+1).cumprod()[-1]

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

  3. Run the doctest by calling the rundocs() function of the numpy.testing module, for instance, in the Python ...

Get NumPy : Beginner's Guide - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.