New in version 2.1.
The Python unit testing framework, sometimes referred to as ``PyUnit,'' isa Python language version of JUnit, by Kent Beck and Erich Gamma.JUnit is, in turn, a Java version of Kent's Smalltalk testingframework. Each is the de facto standard unit testing framework forits respective language.
unittest supports test automation, sharing of setup and shutdowncode for tests, aggregation of tests into collections, and independence ofthe tests from the reporting framework. Theunittest moduleprovides classes that make it easy to support these qualities for aset of tests.
To achieve this,unittest supports some important concepts:
The test case and test fixture concepts are supported through theTestCase andFunctionTestCase classes; the formershould be used when creating new tests, and the latter can be used whenintegrating existing test code with aunittest-driven framework.When building test fixtures usingTestCase, thesetUp()andtearDown() methods can be overridden to provideinitialization and cleanup for the fixture. WithFunctionTestCase, existing functions can be passed to theconstructor for these purposes. When the test is run, thefixture initialization is run first; if it succeeds, the cleanupmethod is run after the test has been executed, regardless of theoutcome of the test. Each instance of theTestCase will onlybe used to run a single test method, so a new fixture is created foreach test.
Test suites are implemented by theTestSuite class. Thisclass allows individual tests and test suites to be aggregated; whenthe suite is executed, all tests added directly to the suite and in``child'' test suites are run.
A test runner is an object that provides a single method,run(), which accepts aTestCase orTestSuiteobject as a parameter, and returns a result object. The classTestResult is provided for use as the result object.unittest provides theTextTestRunner as an exampletest runner which reports test results on the standard error stream bydefault. Alternate runners can be implemented for other environments(such as graphical environments) without any need to derive from aspecific class.
See Also: