1
# Copyright (C) 2004, 2005, 2006 Canonical Ltd
1
# Copyright (c) 2004 Canonical Limited
2
2
# Author: Robert Collins <robert.collins@canonical.com>
4
4
# This program is free software; you can redistribute it and/or modify
76
71
class TestLoader(unittest.TestLoader):
77
"""Custom TestLoader to extend the stock python one."""
72
"""Custom TestLoader to address some quirks in the stock python one."""
79
73
suiteClass = TestSuite
80
# Memoize test names by test class dict
83
75
def loadTestsFromModuleNames(self, names):
84
76
"""use a custom means to load tests from modules.
90
82
result = self.suiteClass()
92
module = _load_module_by_name(name)
93
result.addTests(self.loadTestsFromModule(module))
84
_load_module_by_name(name)
85
result.addTests(self.loadTestsFromName(name))
96
def loadTestsFromModule(self, module):
97
"""Load tests from a module object.
99
This extension of the python test loader looks for an attribute
100
load_tests in the module object, and if not found falls back to the
101
regular python loadTestsFromModule.
103
If a load_tests attribute is found, it is called and the result is
106
load_tests should be defined like so:
107
>>> def load_tests(standard_tests, module, loader):
110
standard_tests is the tests found by the stock TestLoader in the
111
module, module and loader are the module and loader instances.
113
For instance, to run every test twice, you might do:
114
>>> def load_tests(standard_tests, module, loader):
115
>>> result = loader.suiteClass()
116
>>> for test in iter_suite_tests(standard_tests):
117
>>> result.addTests([test, test])
120
basic_tests = super(TestLoader, self).loadTestsFromModule(module)
121
load_tests = getattr(module, "load_tests", None)
122
if load_tests is not None:
123
return load_tests(basic_tests, module, self)
127
def getTestCaseNames(self, test_case_class):
128
test_fn_names = self.test_func_names.get(test_case_class, None)
129
if test_fn_names is not None:
130
# We already calculate that
133
test_fn_names = unittest.TestLoader.getTestCaseNames(self,
135
self.test_func_names[test_case_class] = test_fn_names
138
89
def _load_module_by_name(mod_name):
139
90
parts = mod_name.split('.')