23.3.4 Classes and functions

class TestCase( [methodName])
Instances of the TestCase class represent the smallest testable units in the unittest universe. This class is intended to be used as a base class, with specific tests being implemented by concrete subclasses. This class implements the interface needed by the test runner to allow it to drive the test, and methods that the test code can use to check for and report various kinds of failure.

Each instance of TestCase will run a single test method: the method named methodName. If you remember, we had an earlier example that went something like this:

  def suite():
      suite = unittest.TestSuite()
      return suite

Here, we create two instances of WidgetTestCase, each of which runs a single test.

methodName defaults to 'runTest'.

class FunctionTestCase( testFunc[, setUp[, tearDown[, description]]])
This class implements the portion of the TestCase interface which allows the test runner to drive the test, but does not provide the methods which test code can use to check and report errors. This is used to create test cases using legacy test code, allowing it to be integrated into a unittest-based test framework.

class TestSuite( [tests])
This class represents an aggregation of individual tests cases and test suites. The class presents the interface needed by the test runner to allow it to be run as any other test case. Running a TestSuite instance is the same as iterating over the suite, running each test individually.

If tests is given, it must be an iterable of individual test cases or other test suites that will be used to build the suite initially. Additional methods are provided to add test cases and suites to the collection later on.

class TestLoader( )
This class is responsible for loading tests according to various criteria and returning them wrapped in a TestSuite. It can load all tests within a given module or TestCase subclass.

class TestResult( )
This class is used to compile information about which tests have succeeded and which have failed.

Instance of the TestLoader class intended to be shared. If no customization of the TestLoader is needed, this instance can be used instead of repeatedly creating new instances.

class TextTestRunner( [stream[, descriptions[, verbosity]]])
A basic test runner implementation which prints results on standard error. It has a few configurable parameters, but is essentially very simple. Graphical applications which run test suites should provide alternate implementations.

main( [module[, defaultTest[, argv[, testRunner[, testLoader]]]]])
A command-line program that runs a set of tests; this is primarily for making test modules conveniently executable. The simplest use for this function is to include the following line at the end of a test script:

if __name__ == '__main__':

In some cases, the existing tests may have been written using the doctest module. If so, that module provides a DocTestSuite class that can automatically build unittest.TestSuite instances from the existing doctest-based tests. New in version 2.3.

See About this document... for information on suggesting changes.