71
76
class TestLoader(unittest.TestLoader):
72
"""Custome TestLoader to set the right TestSuite class."""
77
"""Custom TestLoader to extend the stock python one."""
73
79
suiteClass = TestSuite
80
# Memoize test names by test class dict
83
def loadTestsFromModuleNames(self, names):
84
"""use a custom means to load tests from modules.
86
There is an undesirable glitch in the python TestLoader where a
87
import error is ignore. We think this can be solved by ensuring the
88
requested name is resolvable, if its not raising the original error.
90
result = self.suiteClass()
92
result.addTests(self.loadTestsFromModuleName(name))
95
def loadTestsFromModuleName(self, name):
96
result = self.suiteClass()
97
module = _load_module_by_name(name)
99
result.addTests(self.loadTestsFromModule(module))
102
def loadTestsFromModule(self, module):
103
"""Load tests from a module object.
105
This extension of the python test loader looks for an attribute
106
load_tests in the module object, and if not found falls back to the
107
regular python loadTestsFromModule.
109
If a load_tests attribute is found, it is called and the result is
112
load_tests should be defined like so:
113
>>> def load_tests(standard_tests, module, loader):
116
standard_tests is the tests found by the stock TestLoader in the
117
module, module and loader are the module and loader instances.
119
For instance, to run every test twice, you might do:
120
>>> def load_tests(standard_tests, module, loader):
121
>>> result = loader.suiteClass()
122
>>> for test in iter_suite_tests(standard_tests):
123
>>> result.addTests([test, test])
126
basic_tests = super(TestLoader, self).loadTestsFromModule(module)
127
load_tests = getattr(module, "load_tests", None)
128
if load_tests is not None:
129
return load_tests(basic_tests, module, self)
133
def getTestCaseNames(self, test_case_class):
134
test_fn_names = self.test_func_names.get(test_case_class, None)
135
if test_fn_names is not None:
136
# We already know them
139
test_fn_names = unittest.TestLoader.getTestCaseNames(self,
141
self.test_func_names[test_case_class] = test_fn_names
145
class FilteredByModuleTestLoader(TestLoader):
146
"""A test loader that import only the needed modules."""
148
def __init__(self, needs_module):
151
:param needs_module: a callable taking a module name as a
152
parameter returing True if the module should be loaded.
154
TestLoader.__init__(self)
155
self.needs_module = needs_module
157
def loadTestsFromModuleName(self, name):
158
if self.needs_module(name):
159
return TestLoader.loadTestsFromModuleName(self, name)
161
return self.suiteClass()
164
def _load_module_by_name(mod_name):
165
parts = mod_name.split('.')
166
module = __import__(mod_name)
168
# for historical reasons python returns the top-level module even though
169
# it loads the submodule; we need to walk down to get the one we want.
171
module = getattr(module, parts.pop(0))
75
175
class TestVisitor(object):
76
176
"""A visitor for Tests"""