72
72
visitor.visitSuite(self)
73
73
visitTests(self, visitor)
75
def run(self, result):
76
"""Run the tests in the suite, discarding references after running."""
82
self._tests = reversed(tests)
84
tests.pop().run(result)
88
76
class TestLoader(unittest.TestLoader):
89
"""Custom TestLoader to extend the stock python one."""
77
"""Custom TestLoader to address some quirks in the stock python one."""
91
78
suiteClass = TestSuite
92
# Memoize test names by test class dict
95
80
def loadTestsFromModuleNames(self, names):
96
81
"""use a custom means to load tests from modules.
98
There is an undesirable glitch in the python TestLoader where a
99
import error is ignore. We think this can be solved by ensuring the
83
There is an undesirable glitch in the python TestLoader where a
84
import error is ignore. We think this can be solved by ensuring the
100
85
requested name is resolvable, if its not raising the original error.
102
87
result = self.suiteClass()
103
88
for name in names:
104
result.addTests(self.loadTestsFromModuleName(name))
107
def loadTestsFromModuleName(self, name):
108
result = self.suiteClass()
109
module = _load_module_by_name(name)
111
result.addTests(self.loadTestsFromModule(module))
114
def loadTestsFromModule(self, module):
115
"""Load tests from a module object.
117
This extension of the python test loader looks for an attribute
118
load_tests in the module object, and if not found falls back to the
119
regular python loadTestsFromModule.
121
If a load_tests attribute is found, it is called and the result is
124
load_tests should be defined like so:
125
>>> def load_tests(standard_tests, module, loader):
128
standard_tests is the tests found by the stock TestLoader in the
129
module, module and loader are the module and loader instances.
131
For instance, to run every test twice, you might do:
132
>>> def load_tests(standard_tests, module, loader):
133
>>> result = loader.suiteClass()
134
>>> for test in iter_suite_tests(standard_tests):
135
>>> result.addTests([test, test])
138
if sys.version_info < (2, 7):
139
basic_tests = super(TestLoader, self).loadTestsFromModule(module)
141
# GZ 2010-07-19: Python 2.7 unittest also uses load_tests but with
142
# a different and incompatible signature
143
basic_tests = super(TestLoader, self).loadTestsFromModule(module,
144
use_load_tests=False)
145
load_tests = getattr(module, "load_tests", None)
146
if load_tests is not None:
147
return load_tests(basic_tests, module, self)
151
def getTestCaseNames(self, test_case_class):
152
test_fn_names = self.test_func_names.get(test_case_class, None)
153
if test_fn_names is not None:
154
# We already know them
157
test_fn_names = unittest.TestLoader.getTestCaseNames(self,
159
self.test_func_names[test_case_class] = test_fn_names
163
class FilteredByModuleTestLoader(TestLoader):
164
"""A test loader that import only the needed modules."""
166
def __init__(self, needs_module):
169
:param needs_module: a callable taking a module name as a
170
parameter returing True if the module should be loaded.
172
TestLoader.__init__(self)
173
self.needs_module = needs_module
175
def loadTestsFromModuleName(self, name):
176
if self.needs_module(name):
177
return TestLoader.loadTestsFromModuleName(self, name)
179
return self.suiteClass()
89
_load_module_by_name(name)
90
result.addTests(self.loadTestsFromName(name))
182
94
def _load_module_by_name(mod_name):