Unit Test in Python

Python Unit Test is something you will encounter in your intermediate term of python journey, Its good practice to follow unit test as it allows you to test a single unit of logic. I am not going into too much depth of advantage and disadvantages of unit test and why you should use it, you can find many more posts on the topic. My focus here to show you some real code quickly which helps you to build a quick understanding of the topic.
Following is the simple program we will be using to see unit test in reality. Just go through it and I will provide a quick explanation of steps. Detailed step by step explanation exist at the end of this post.

#!/usr/bin/env python
class Calculate:
    def add(self, x, y):
        return x + y

if __name__ == '__main__':
    calc = Calculate()
    result = calc.add(2,3)
    print result

Unit test Part:
First import unit test libraries
import unittest

Then import your program:
from calc import Calculator

Now you need to create a class say and override with `unittest.Testcase`
class TestCalculate(unittest.Testcase):

Under this class create few methods. First of all you will define some setUp methods as below:
class TestCalculate(unittest.Testcase):
    def setUp(self):
        self.calc = Calculate()

In above code snippet you basically instantiated you class as self.calc, this is all covered under setup menthod, basically you are setting up your environment.

Next, we will create further few method, that will basically perform our real test of the unit of our interest.

    def test_addition(self):
        self.assertEqual(5, calc.add(2, 3))

Complete code looks like:

#!/usr/bin/env python

import unittest
from calc import Calculate

class TestCalculate(unittest.TestCase):
    def setUp(self):
        self.calc = Calculate()
    def test_addition(self):
        self.assertEqual(5, self.calc.add(2,3))

if __name__ == "__main__":

A line-by-line examination shows that this example:
  • First imports the functionality you need from Python’s unittest module. You are also importing your own class, Calculate, so that you can test its methods. You do this in the setUp method, which is executed before each test, so that you need to define your instance only once and have it created before each test. 
  • Then you can write your test and again the standard is to append your test name with append_testand explain what the test is doing briefly in the rest of the name. Here you are checking if the add method returns the correct result. To do this, you make use of the assertEqual method provided by the imported unittest module. This checks if the first argument is equal to the second. In this example, you are checking whether 5 is equal to the result of calling your add method on 2 and 3. In this case, the test passes as your code works and displays the following result in the terminal.

Popular posts from this blog

Grsecurity: Configure RHEL5/6 Kernel for Grsecurity

Linux Command line tips and Bash stuff

Using MTS MBlaze on Mint Linux 14 / Ubuntu 12.04