Previous: 5.3.1 Organizing test code Up: 5.3 unittest Next: 5.3.3 Classes and functions


5.3.2 Re-using old test code

Some users will find that they have existing test code that they would like to run from PyUnit, without converting every old test function to a TestCase subclass.

For this reason, PyUnit provides a FunctionTestCase class. This subclass of TestCase can be used to wrap an existing test function. Set-up and tear-down functions can also optionally be wrapped.

Given the following test function:

def testSomething():
 something = makeSomething()
 assert something.name is not None
 # ...

one can create an equivalent test case instance as follows:

testcase = unittest.FunctionTestCase(testSomething)

If there are additional set-up and tear-down methods that should be called as part of the test case's operation, they can also be provided:

testcase = unittest.FunctionTestCase(testSomething,
 setUp=makeSomethingDB,
 tearDown=deleteSomethingDB)

Note: PyUnit supports the use of AssertionError as an indicator of test failure, but does not recommend it. Future versions may treat AssertionError differently.


Previous: 5.3.1 Organizing test code Up: 5.3 unittest Next: 5.3.3 Classes and functions
Release 2.2.3, documentation updated on 30 May 2003.
See About this document... for information on suggesting changes.

AltStyle によって変換されたページ (->オリジナル) /