Introduction to the unittest Framework in Python

unittest is a Python framework for writing Unit Tests, Integration Tests, and Acceptance Tests. It mainly provides a class TestCase and a main() method.

unittest is typically imported with:

  1. from unittest import TestCase, main

Writing a test class

Test classes should extend TestCase, and contain at least one method starting with test_ . Test methods contain assertions.

TestCase offers many assertion methods (assertEqual, assertAlmostEqual, assertTrue etc.).

  1. class AdditionTests(TestCase):
  2. def test_add(self):
  3. self.assertEqual(add(3, 4), 7)

Running the tests

The unittest.main method will look for all classes derived from TestCase that have been imported. It runs all tests inside them and reports.

Typically, you will find main() called in a separate code block:

  1. if __name__ == '__main__':
  2. main()

You can run Python test files with unittest without calling main()

  1. python -m unittest test_file

Note: The name of the test module is spelled without .py

Testing command-line scripts

To test a command-line script call it using a shell command and redirect the output for further evaluation. The simplest way is to use os.system:

  1. import os
  2. os.system('python myprog.py > out.txt')

Discovering tests

  1. python -m unittest discover

Test data and fixtures

The methods setUp() and tearDown() can be used to prepare testing and clean up afterwards.

Importing test data in multiple packages

When you have many tests distributed to sub-packages, you may want to share test data among them. There are two ways to do so:

Either set the PYTHONPATH variable to the directory with your tests.

Alternatively, patch sys.path in a local module test_data.py in each of the sub-packages, so that they import ../test_data.*