Unit testing in Python: Part 2

Harsh Motiramani
2 min readDec 3, 2021

--

In my last blog about Unit testing we talked about what is unit testing and why do we need it, we also implemented it to some extent. This blog is its continuation. To understand this one you should first go through my first part of Unit testing.

And in this blog, we will implement unit testing and see outputs of two scenarios in a code.

So I had suggested file names for both the files which are square_area_test.py and square_area.py . Now we will add the unit testing code.

The main operation will be in the square_area.py file and the code is below:

def square_area(s):
return (s*s)

We will start to test the code in the square_area_test.py file by adding the first line:

import unittest

Then we add a class called TestSquare where we define different various methods and to create different situations we use assertAlmostEqual method. Below is the code for further steps in testing:

from square_area import*
import unittest
class TestSquare(unittest.TestCase):
def test_area(self):
self.assertAlmostEqual(square_area(9),9)
self.assertAlmostEqual(square_area(0),0)
self.assertAlmostEqual(square_area(1),1)
self.assertAlmostEqual(square_area(1.9),1,9)

Before testing the above script, run the unittest module with specifying -m in the code.

!python -m unittest square_area_test.py

The output must show you “OK” which means the test was successful.

What if we make some changes in an assertion sentence?

from square_area import*
import unittest
class TestSquare(unittest.TestCase):
def test_area(self):
self.assertAlmostEqual(square_area(9),9)
self.assertAlmostEqual(square_area(0),0)
self.assertAlmostEqual(square_area(1),1)
self.assertAlmostEqual(square_area(1.9),0)

In the last line, I replaced 1.9with 0 and when we run this test. Again specifying the -m.

!python -m unittest square_area_test.py

The output will show you “F” which means failed and also it will show AssertionError hence this test was a failure. Besides the error statement, you might see the reason for which the error occurred and can resolve it accordingly. These were two types of outputs expected while testing.

Before we end this blog there is one more important note the user should learn about. In the code, we have designed, the def function should always start with the keyword test like I have used test_area .

Let’s see what happens if we dont.

from square_area import*
import unittest
class TestSquare(unittest.TestCase):
def area(self):
self.assertAlmostEqual(square_area(9),9)
self.assertAlmostEqual(square_area(0),0)
self.assertAlmostEqual(square_area(1),1)
self.assertAlmostEqual(square_area(1.9),1.9)

I have removed the test keyword and only kept area. Now we run it

!python -m unittest test_volume_cuboid.py

The output you see will show “OK” and also “Ran 1 test” this means it ran only 1 test and as it was correct it gave the output, this is why we always add the test keyword or your script isn't fully verified.

Reference: https://www.datacamp.com/community/tutorials/unit-testing-python

So these were some scenarios and ways to test your script. See you in the next one!

--

--