UnitTest – Framework

UnitTest - Framework

UnitTest Framework 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.

The unit test module provides classes that make it easy to support these qualities for a set of tests.

To achieve this, unit test supports the following important concepts โˆ’

  • test fixture โˆ’ This represents the preparation needed to perform one or more tests, and any associate cleanup actions. This may involve, for example, creating temporary or proxy databases, directories, or starting a server process.
  • test case โˆ’ This is the smallest unit of testing. This checks for a specific response to a particular set of inputs. unittest provides a base class, TestCase, which may be used to create new test cases.
  • test suite โˆ’ This is a collection of test cases, test suites, or both implemented by the TestSuite class.
  • test runner โˆ’ This is a component which orchestrates the execution of tests and provides the outcome to the user. The runner may use a graphical interface, a textual interface, or return a special value to indicate the results of executing the tests.

Creating a UnitTest Framework –

The following steps unit test โˆ’

Step 1 โˆ’ Import the unit test module into your program.

Step 2 โˆ’ Define a function to be tested. In the following example, add() function is to be subjected to test.

Step 3 โˆ’ Create a test case by subclassing the unit test.TestCase.

Step 4 โˆ’ Define a test as a method inside the class. The name of the method must start with ‘test’.

Step 5 โˆ’ Each test calls the assert function of TestCase class. There are many types of assets. Following example calls assertEquals() function.

Step6 โˆ’ assertEquals() function compares result of add() function with arg2 argument and throws assertionError if comparison fails.

Step7 โˆ’ Finally, call the main() method from the unit test module.

import unittest
def add(x,y):
   return x + y
   
class SimpleTest(unittest.TestCase):
   def testadd1(self):
      self.assertEquals(add(4,5),9)
      
if __name__ == '__main__':
   unittest.main()

Step8 โˆ’ Run the above script from the command line.

C:\Python27>python SimpleTest.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK

Step 9ย โˆ’ The following three tests โˆ’

Sr.NoMessage & Description
1OK, The test passes. โ€˜Aโ€™ is displayed on the console.
2FAIL The test does not pass and raises an AssertionError exception. โ€˜Fโ€™ is displayed on the console.
3ERROR The test raises an exception other than AssertionError. โ€˜Eโ€™ is displayed on the console.

These outcomes are displayed on the console by ‘.’, ‘F’, and ‘E’ respectively.

Command Line Interface

The unit test module can be used from the command line to run single or multiple tests.

python -m unittest test1
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method

Unit test supports the following command-line options. For a list of all the command-line options, use the following command โˆ’

Python โ€“m unittest -h
Sr.NoOption & Description
1-h, –helpShow this message
2v, –verbose output
3-q, –quietMinimal output
4-f, –failfastStop on the first failure
5-c, –catch control-C and display results
6-b, –buffer stdout and stderr during test runs

Next Topic – Click Here

This Post Has 2 Comments

Leave a Reply