/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/TestUtil.py

  • Committer: Robert Collins
  • Date: 2008-02-06 04:06:42 UTC
  • mfrom: (3216 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3217.
  • Revision ID: robertc@robertcollins.net-20080206040642-2efx3l4iv5f95lxp
Merge up with bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
74
74
 
75
75
 
76
76
class TestLoader(unittest.TestLoader):
77
 
    """Custom  TestLoader to address some quirks in the stock python one."""
 
77
    """Custom TestLoader to extend the stock python one."""
 
78
 
78
79
    suiteClass = TestSuite
 
80
    # Memoize test names by test class dict
 
81
    test_func_names = {}
79
82
 
80
83
    def loadTestsFromModuleNames(self, names):
81
84
        """use a custom means to load tests from modules.
86
89
        """
87
90
        result = self.suiteClass()
88
91
        for name in names:
89
 
            _load_module_by_name(name)
90
 
            result.addTests(self.loadTestsFromName(name))
 
92
            module = _load_module_by_name(name)
 
93
            result.addTests(self.loadTestsFromModule(module))
91
94
        return result
92
95
 
 
96
    def loadTestsFromModule(self, module):
 
97
        """Load tests from a module object.
 
98
 
 
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.
 
102
 
 
103
        If a load_tests attribute is found, it is called and the result is
 
104
        returned. 
 
105
 
 
106
        load_tests should be defined like so:
 
107
        >>> def load_tests(standard_tests, module, loader):
 
108
        >>>    pass
 
109
 
 
110
        standard_tests is the tests found by the stock TestLoader in the
 
111
        module, module and loader are the module and loader instances.
 
112
 
 
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])
 
118
        >>>     return result
 
119
        """
 
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)
 
124
        else:
 
125
            return basic_tests
 
126
 
 
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
 
131
            return test_fn_names
 
132
 
 
133
        test_fn_names = unittest.TestLoader.getTestCaseNames(self,
 
134
                                                             test_case_class)
 
135
        self.test_func_names[test_case_class] = test_fn_names
 
136
        return test_fn_names
93
137
 
94
138
def _load_module_by_name(mod_name):
95
139
    parts = mod_name.split('.')