~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/TestUtil.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-12-19 09:10:53 UTC
  • mfrom: (3128.2.2 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20071219091053-81xgp971m1pgmccf
(robertc) Allow arbitrary parameteriZation of tests cleanly.

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
79
80
 
80
81
    def loadTestsFromModuleNames(self, names):
86
87
        """
87
88
        result = self.suiteClass()
88
89
        for name in names:
89
 
            _load_module_by_name(name)
90
 
            result.addTests(self.loadTestsFromName(name))
 
90
            module = _load_module_by_name(name)
 
91
            result.addTests(self.loadTestsFromModule(module))
91
92
        return result
92
93
 
 
94
    def loadTestsFromModule(self, module):
 
95
        """Load tests from a module object.
 
96
 
 
97
        This extension of the python test loader looks for an attribute
 
98
        load_tests in the module object, and if not found falls back to the
 
99
        regular python loadTestsFromModule.
 
100
 
 
101
        If a load_tests attribute is found, it is called and the result is
 
102
        returned. 
 
103
 
 
104
        load_tests should be defined like so:
 
105
        >>> def load_tests(standard_tests, module, loader):
 
106
        >>>    pass
 
107
 
 
108
        standard_tests is the tests found by the stock TestLoader in the
 
109
        module, module and loader are the module and loader instances.
 
110
 
 
111
        For instance, to run every test twice, you might do:
 
112
        >>> def load_tests(standard_tests, module, loader):
 
113
        >>>     result = loader.suiteClass()
 
114
        >>>     for test in iter_suite_tests(standard_tests):
 
115
        >>>         result.addTests([test, test])
 
116
        >>>     return result
 
117
        """
 
118
        basic_tests = super(TestLoader, self).loadTestsFromModule(module)
 
119
        load_tests = getattr(module, "load_tests", None)
 
120
        if load_tests is not None:
 
121
            return load_tests(basic_tests, module, self)
 
122
        else:
 
123
            return basic_tests
 
124
 
93
125
 
94
126
def _load_module_by_name(mod_name):
95
127
    parts = mod_name.split('.')