23.3.8 TestLoader Objects
The TestLoader class is used to create test suites from
classes and modules. Normally, there is no need to create an instance
of this class; the unittest module provides an instance
that can be shared as unittest.defaultTestLoader
.
Using a subclass or instance, however, allows customization of some
configurable properties.
TestLoader objects have the following methods:
- Return a suite of all tests cases contained in the TestCase-derived testCaseClass.
-
Return a suite of all tests cases contained in the given module.
This method searches module for classes derived from
TestCase and creates an instance of the class for each test
method defined for the class.
Warning: While using a hierarchy of TestCase-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.
-
Return a suite of all tests cases given a string specifier.
The specifier name is a ``dotted name'' that may resolve either to a module, a test case class, a test method within a test case class, a TestSuite instance, or a callable object which returns a TestCase or TestSuite 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 module SampleTests containing a TestCase-derived class SampleTestCase with three test methods (test_one(), test_two(), and test_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 the test_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 resolves name relative to the given module.
- Similar to loadTestsFromName(), 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.
- Return a sorted sequence of method names found within testCaseClass; this should be a subclass of TestCase.
The following attributes of a TestLoader can be configured either by subclassing or assignment on an instance:
-
String giving the prefix of method names which will be interpreted
as test methods. The default value is
'test'
.This affects getTestCaseNames() and all the loadTestsFrom*() methods.
- Function to be used to compare method names when sorting them in getTestCaseNames() and all the loadTestsFrom*() methods. The default value is the built-in cmp() function; the attribute can also be set to None to disable the sort.
-
Callable object that constructs a test suite from a list of tests.
No methods on the resulting object are needed. The default value is
the TestSuite class.
This affects all the loadTestsFrom*() methods.