What is Python Unit Testing?

PythonServer Side ProgrammingProgramming

What is unit testing?

Unit testing is a type of software testing where each individual component of the system is tested. Unit testing is important practice for the developers. It ensures that every component of the software is functioning appropriately as expected. Unit testing is mainly performed by the developers during the coding phase of the software development.

Unit testing makes it easy to fix the problems as the developers come to know which particular component of the system or software has the issues and the developers can fix that particular unit.

Python Unit Testing

The python has an in-built package called unittest which is used to perform unit testing. Unit testing makes the code future proof as the developers anticipate the possible cases where the code may fail and the code is tested against those cases.Even though, we may miss upon certain cases but still majority of the cases will be addressed and the code will be tested against those cases.

The unittest module can be used by importing this module in python program as follows.

import unittest


Suppose, we have a simple function, which calculates area of a square. Let this file be saved with name ‘area.py’.

def area(a):
   return (a+a)

Now, let’s write the unit test code for the above function, which will let us know if our function gives the expected output. Let the unit test code file be saved with the name “unit_test.py”.

unit_test.py &mnus;

from area import *
import unittest

class Testarea(unittest.TestCase):
   def test_area(self):

Here, the file which contains the code to be tested is imported. The unittest method assertAlmostEqual() is used. This calls the area function with some input and the second parameter contains the expected output to be returned by the function. If the output returned by the area() is equal to the expected output, then the test is passed, else it is failed.

Now let’s run the unit_test.py from the command prompt to check if our area() code passes the test.

Execute the following command from command prompt to run the unit_test file.

python -m unittest unit_test.py


C:\Users\Inderjit Singh\Desktop>python -m unittest unit_test.py
FAIL: test_area (unit_test.Testarea)
Traceback (most recent call last):
   File "C:\Users\Inderjit Singh\Desktop\unit_test.py", line 7, in test_area
AssertionError: 10 != 25 within 7 places (15 difference)

Ran 1 test in 0.001s

FAILED (failures=1)

Our code has failed the unit testing. The cause is given in the traceback. The first function self.assertAlmostEqual(area(5),25) causes our code to fail. Since the function returns 10 but the expected output is 25.

Since, this is an easy function, we know the fix for this. Our area() code is wrong. It should be returning (a*a) instead of (a+a).

Let’s fix the area() code and re run the unit test.

def area(a):
   return (a*a)

Re-run the unit_test.py

C:\Users\Inderjit Singh\Desktop>python -m unittest unit_test.py
Ran 1 test in 0.000s


Now, since we have corrected the code in area(), it runs successfully with OK status. This ensures that our code runs perfectly against all the cases specified in the unit_test.py file.

This was a very simple idea to give an idea about the unit testing. There are various other functions in the unittest module which are used in the unit testing in the development process.

Published on 11-Jun-2021 12:32:02