The Python Unit Testing Framework PyUnit is a python language version of JUnit.
The basic building blocks of unit testing are ‘test cases’ — single scenarios that must be set up and checked for correctness. In PyUnit, test cases are represented by the TestCase class in the unittest module. To make your own test cases you must write subclasses of TestCase. An instance of a TestCase class is an object that can completely run a single test method, together with optional set-up and tidy-up code.
The testing code of a TestCase instance should be entirely self contained, such that it can be run either in isolation or in arbitrary combination with any number of other test cases.
unittest supports some important concepts
test fixture–A test fixture represents the preparation needed to perform one or more tests, and any associate cleanup actions.
test case–A test case is the smallest unit of testing. It checks for a specific response to a particular set of inputs.
test suite–A test suite is a collection of test cases, test suites, or both. It is used to aggregate tests that should be executed together.
test runner–A test runner is a component which orchestrates the execution of tests and provides the outcome to the user.
unittest is an object oriented framework based around test fixtures. In unittest the test fixture is the TestCase class, and its basic usage is very simple:
for eg:
import unittest
class MyTest(unittest.TestCase):
def testMethod(self):
self.assertEqual(1 * 2, 2, "1 * 2 not equal to 2")
if __name__ == '__main__':
unittest.main()