Time for action – writing a unit test

We will write tests for a simple factorial function. The tests will check for the so-called happy path and abnormal conditions.

  1. Start by writing the factorial function:
    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.

  2. Now we will write the unit test. Let's write a class that will contain the unit tests. It extends the TestCase class from the unittest module, which is part of standard Python. Test for calling the factorial function with the following three attributes:
    • a positive number, the happy path
    • boundary condition 0
    • negative numbers, which should result in an error
      class FactorialTest(unittest.TestCase):
         def test_factorial(self):
            #Test for the factorial of 3 that should pass.
            self.assertEqual(6, factorial(3)[-1])
            np.testing.assert_equal(np.array([1, 2, 6]), factorial(3))
      
         def test_zero(self):
            #Test for the factorial of 0 that should pass.
            self.assertEqual(1, factorial(0))
      
         def test_negative(self):
            #Test for the factorial of negative numbers that should fail.
            # It should throw a ValueError, but we expect IndexError
            self.assertRaises(IndexError, factorial(-10))

      We rigged one of the tests to fail, as you can see in the following output:

      $ python unit_test.py
      .E.
      ======================================================================
      ERROR: test_negative (__main__.FactorialTest)
      ----------------------------------------------------------------------
      Traceback (most recent call last):
        File "unit_test.py", line 26, in test_negative
          self.assertRaises(IndexError, factorial(-10))
        File "unit_test.py", line 9, in factorial
          raise ValueError, "Unexpected negative value"
      ValueError: Unexpected negative value
      
      ----------------------------------------------------------------------
      Ran 3 tests in 0.003s
      
      FAILED (errors=1)
      

What just happened?

We made some happy path tests for the factorial function code. We let the boundary condition test fail on purpose (see unit_test.py):

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()


class FactorialTest(unittest.TestCase):
   def test_factorial(self):
      #Test for the factorial of 3 that should pass.
      self.assertEqual(6, factorial(3)[-1])
      np.testing.assert_equal(np.array([1, 2, 6]), factorial(3))

   def test_zero(self):
      #Test for the factorial of 0 that should pass.
      self.assertEqual(1, factorial(0))

   def test_negative(self):
      #Test for the factorial of negative numbers that should fail.
      # It should throw a ValueError, but we expect IndexError
      self.assertRaises(IndexError, factorial(-10))


if __name__ == '__main__':
    unittest.main()
..................Content has been hidden....................

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