June 2015
Beginner
348 pages
8h 44m
English
We will write tests for a simple factorial function. The tests will check for the so-called happy path and abnormal conditions.
import numpy as np
import unittest
def factorial(n):
if n == 0:
return 1
if n < 0:
raise ValueError, "Unexpected negative value"
return np.arange(1, n+1).cumprod()The code uses the arange() and cumprod() functions to create arrays and calculate the cumulative product, but we added a few checks for boundary conditions.
TestCase class from the unittest module, which is part of standard Python. Test for calling the factorial function with the following ...Read now
Unlock full access