Refresh

This website www.otcollect.com/get-started-with-python-unit-testing/tl/11579440-7d00-4341-9db4-f08b068fdad8 is currently offline. Cloudflare's Always Online™ shows a snapshot of this web page from the Internet Archive's Wayback Machine. To check for the live version, click Refresh.

Get Started With Python Unit Testing

When writing a program, it is our intention that the end program runs as we intended in production. One way to ensure the programs we write will do exactly what we intended is to write unit test code for such programs.

 

Python comes with a built in unit testing library known as unittest. In this tutorial, we utilize this library to show you how to write simple efficient test for your Python program.

 

To get started, we first create a module known as math.py. In this module, we will have a couple of Arithmetic functions such as addition, subtraction, division etc.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
"""Module for simple Math applications"""
def add(a,b):
  """This funtions returns the sum of two inputs {a} and {b}"""
  return a + b

def minus(a,b):
  """This funtions returns the subtraction of one input {b} from input {a}"""
  return a - b

def divide(a,b):
  """This funtions returns the division of two inputs {a} and {b}"""
  return a % b

Unit testing is a systematic and reproducible way to test your code used to validate if code units are operating as designed.

 

Unit in "unit testing" represents the smallest testable part of an application, which in our case here will be the functions add, minus, and divide. Typically units can be functions and modules.

 

The units(functions) we will be testing are add, minus, and divide, as defined in the math.py module. It is important to note that the naming convention for the testing modules should always be with the prefix test_.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import unittest
from math import add, minus, divide

class TestAdd(unittest.TestCase): 
    def test1(self): 
        self.assertEqual(add(1,2), 4) # test when 1, 2 is given as input the output is 3.
        self.assertEqual(add(3.0), 9.0)  # test when 2.5, 2.5 is given as input the output is 5.

class TestMinus(unittest.TestCase): 
    def test1(self): 
        self.assertEqual(minus(4,2), 2) # test when 4,2 is given as input the output is 2.
        self.assertEqual(minus(6, 2), 4)  # test when 6, 2 is given as input the output is 4.

class TestDivide(unittest.TestCase): 
    def test1(self): 
        self.assertEqual(divide(4,2), 2) # test when 4,2 is given as input the output is 2.
        self.assertEqual(divide(5, 2), 2.5)  # test when 5, 2 is given as input the output is 2.5.

 

Commonly used TestCase assert methods:

The TestCase class provides several assert methods to check for and report failures. The following table lists the most commonly used methods (see the tables below for more assert methods):

Try it out here:

If you have time, you can learn more about python unit testing from the Python Documentations https://docs.python.org/3/library/unittest.html

0 Comments

12345

    00