Unittest – Unit Testing Framework (Python)

Unit testing : It refers to the kind of testing where the tester refers to a small software module at a time and testing it in an isolated fashion.

Unittest supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework module.This module provides classes that make it easy to support these qualities for a set of tests.

Test Fixture : A test fixture is setting up well known and fixed environment in which tests are run so that to get a particular/expected  outcome.

A test is generally done in four phases.

Four Phases of a test are :

  • set up – It is used for setting up test fixture.
  • Exercise – interact with system under test.
  • verify – determine whether the expected outcome has been obtained.
  • Tear down – for cleanup of test fixture so that it returns to original state.

Note : Unit testing is a kind of white box testing.

Here I am giving a brief introduction  to unitttesting (python) so that those who wants to get started with software testing will find it interesting and easier.

I have taken a simple example of  a calculator application and will try to test it through unit testing frame work provided by unittest module in python.

# calculator.py

class Calculator:
def add(self, x, y):
return x + y

def sub(self, x, y):
return x - y

def mul(self, x, y):
return x * y

def div(self, x, y):
assert (y != 0)
        return x / y

The following is the unittest code for testing this calculator application.


# test_calculator.py

import unittest
from calculator import Calculator

class TestCalculator(unittest.TestCase):
def setUp(self):
self.cal = Calculator()

def test_add(self):
res = self.cal.add(10, 2)
self.assertEqual(12, res)

def test_sub(self):
res = self.cal.sub(7, 4)
self.assertEqual(3, res)

def test_mul(self):
res = self.cal.mul(5, 25)
self.assertEqual(125, res)

def test_div(self):
res = self.cal.div(20, 4)
self.assertEqual(5, res)

if __name__ == '__main__':
unittest.main()

In unittest framework we have to create a subclass of unittest TestCase as I have create in the above code. Here TestCalculator is the subclass of unittest TestCase class. The important thing to note here is that the name of the testcase class start with the word Test(i.e should follow this pattern ‘ Test*’ ) and the test methods name follows the pattern ‘ test* ‘. The methods test_add, test_sub, test_mul, test_div is used for testing add(), sub() , mul(), div() methods of Calculator class.Then setUp() method is used to create test fixture for the tests.

Skipping tests and Expected failures:

unittest provides methods for skipping test methods as well as test class and to mark a test as expected failures when we know that the test is going to fail.Here is an example code showing test skipping and expected failures.

# test_calculator.py

import unittest
from calculator import Calculator

class TestCalculator(unittest.TestCase):
def setUp(self):
self.cal = Calculator()

def test_add(self):
res = self.cal.add(10, 2)
self.assertEqual(12, res)

@unittest.skip("Demonstrating method skippping")
def test_sub(self):
res = self.cal.sub(7, 4)
self.assertEqual(12, res)

    @unittest.skipIf(2 > 0, "Demonstrating method skipping using skipIf")
def test_mul(self):
res = self.cal.mul(5, 25)
self.assertEqual(125, res)

@unittest.expectedFailure
def test_div(self):
res = self.cal.div(20, 4)
self.assertEqual(5, res)

if __name__ == '__main__':
unittest.main()

Leave a comment