Movatterモバイル変換


[0]ホーム

URL:


Up one LevelPython Library ReferenceContentsModule IndexIndex

23.3unittest -- Unit testing framework

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:

test fixture
Atest fixture represents the preparation needed to perform oneor more tests, and any associate cleanup actions. This may involve,for example, creating temporary or proxy databases, directories, orstarting a server process.

test case
Atest case is the smallest unit of testing. It checks for aspecific response to a particular set of inputs.unittestprovides a base class,TestCase, which may be used to createnew test cases.

test suite
Atest suite is a collection of test cases, test suites, orboth. It is used to aggregate tests that should be executedtogether.

test runner
Atest runner is a component which orchestrates the execution oftests and provides the outcome to the user. The runner may use agraphical interface, a textual interface, or return a special value toindicate the results of executing the tests.

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:

Moduledoctest:
Another test-support module with a very different flavor.
Simple Smalltalk Testing: With Patterns
Kent Beck's original paper on testing frameworks using the pattern shared byunittest.



Subsections


Up one LevelPython Library ReferenceContentsModule IndexIndex

Release 2.5.2, documentation updated on 21st February, 2008.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2025 Movatter.jp