Movatterモバイル変換


[0]ホーム

URL:


Up one LevelPython Library ReferenceContentsModule IndexIndex


23.3.8 TestLoader Objects

TheTestLoader class is used to create test suites fromclasses and modules. Normally, there is no need to create an instanceof this class; theunittest module provides an instancethat can be shared asunittest.defaultTestLoader.Using a subclass or instance, however, allows customization of someconfigurable properties.

TestLoader objects have the following methods:

loadTestsFromTestCase(testCaseClass)
Return a suite of all tests cases contained in theTestCase-derivedtestCaseClass.

loadTestsFromModule(module)
Return a suite of all tests cases contained in the given module. This method searchesmodule for classes derived fromTestCase and creates an instance of the class for each test method defined for the class.

Warning:While using a hierarchy ofTestCase-derived classes can be convenient in sharing fixtures and helper functions, defining test methods on base classes that are not intended to be instantiated directly does not play well with this method. Doing so, however, can be useful when the fixtures are different and defined in subclasses.

loadTestsFromName(name[, module])
Return a suite of all tests cases given a string specifier.

The specifiername is a ``dotted name'' that may resolve either to a module, a test case class, a test method within a test case class, aTestSuite instance, or a callable object which returns aTestCase orTestSuite instance. These checks are applied in the order listed here; that is, a method on a possible test case class will be picked up as ``a test method within a test case class'', rather than ``a callable object''.

For example, if you have a moduleSampleTests containing aTestCase-derived classSampleTestCase with three test methods (test_one(),test_two(), andtest_three()), the specifier'SampleTests.SampleTestCase' would cause this method to return a suite which will run all three test methods. Using the specifier'SampleTests.SampleTestCase.test_two' would cause it to return a test suite which will run only thetest_two() test method. The specifier can refer to modules and packages which have not been imported; they will be imported as a side-effect.

The method optionally resolvesname relative to the givenmodule.

loadTestsFromNames(names[, module])
Similar toloadTestsFromName(), but takes a sequence of names rather than a single name. The return value is a test suite which supports all the tests defined for each name.

getTestCaseNames(testCaseClass)
Return a sorted sequence of method names found withintestCaseClass; this should be a subclass ofTestCase.

The following attributes of aTestLoader can be configuredeither by subclassing or assignment on an instance:

testMethodPrefix
String giving the prefix of method names which will be interpreted as test methods. The default value is'test'.

This affectsgetTestCaseNames() and all theloadTestsFrom*() methods.

sortTestMethodsUsing
Function to be used to compare method names when sorting them ingetTestCaseNames() and all theloadTestsFrom*() methods. The default value is the built-incmp() function; the attribute can also be set toNone to disable the sort.

suiteClass
Callable object that constructs a test suite from a list of tests. No methods on the resulting object are needed. The default value is theTestSuite class.

This affects all theloadTestsFrom*() methods.


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