1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
2
 
#       Author: Robert Collins <robert.collins@canonical.com>
 
4
 
# This program is free software; you can redistribute it and/or modify
 
5
 
# it under the terms of the GNU General Public License as published by
 
6
 
# the Free Software Foundation; either version 2 of the License, or
 
7
 
# (at your option) any later version.
 
9
 
# This program is distributed in the hope that it will be useful,
 
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 
# GNU General Public License for more details.
 
14
 
# You should have received a copy of the GNU General Public License
 
15
 
# along with this program; if not, write to the Free Software
 
16
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
23
 
# Mark this python module as being part of the implementation
 
24
 
# of unittest: this gives us better tracebacks where the last
 
25
 
# shown frame is the test code, not our assertXYZ.
 
29
 
class LogCollector(logging.Handler):
 
31
 
        logging.Handler.__init__(self)
 
33
 
    def emit(self, record):
 
34
 
        self.records.append(record.getMessage())
 
37
 
def makeCollectingLogger():
 
38
 
    """I make a logger instance that collects its logs for programmatic analysis
 
39
 
    -> (logger, collector)"""
 
40
 
    logger=logging.Logger("collector")
 
41
 
    handler=LogCollector()
 
42
 
    handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
 
43
 
    logger.addHandler(handler)
 
44
 
    return logger, handler
 
47
 
def visitTests(suite, visitor):
 
48
 
    """A foreign method for visiting the tests in a test suite."""
 
49
 
    for test in suite._tests:
 
50
 
        #Abusing types to avoid monkey patching unittest.TestCase.
 
51
 
        # Maybe that would be better?
 
54
 
        except AttributeError:
 
55
 
            if isinstance(test, unittest.TestCase):
 
56
 
                visitor.visitCase(test)
 
57
 
            elif isinstance(test, unittest.TestSuite):
 
58
 
                visitor.visitSuite(test)
 
59
 
                visitTests(test, visitor)
 
61
 
                print "unvisitable non-unittest.TestCase element %r (%r)" % (test, test.__class__)
 
64
 
class TestSuite(unittest.TestSuite):
 
65
 
    """I am an extended TestSuite with a visitor interface.
 
66
 
    This is primarily to allow filtering of tests - and suites or
 
67
 
    more in the future. An iterator of just tests wouldn't scale..."""
 
69
 
    def visit(self, visitor):
 
70
 
        """visit the composite. Visiting is depth-first.
 
71
 
        current callbacks are visitSuite and visitCase."""
 
72
 
        visitor.visitSuite(self)
 
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
 
class TestLoader(unittest.TestLoader):
 
89
 
    """Custom TestLoader to extend the stock python one."""
 
91
 
    suiteClass = TestSuite
 
92
 
    # Memoize test names by test class dict
 
95
 
    def loadTestsFromModuleNames(self, names):
 
96
 
        """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
 
100
 
        requested name is resolvable, if its not raising the original error.
 
102
 
        result = self.suiteClass()
 
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
 
        basic_tests = super(TestLoader, self).loadTestsFromModule(module)
 
139
 
        load_tests = getattr(module, "load_tests", None)
 
140
 
        if load_tests is not None:
 
141
 
            return load_tests(basic_tests, module, self)
 
145
 
    def getTestCaseNames(self, test_case_class):
 
146
 
        test_fn_names = self.test_func_names.get(test_case_class, None)
 
147
 
        if test_fn_names is not None:
 
148
 
            # We already know them
 
151
 
        test_fn_names = unittest.TestLoader.getTestCaseNames(self,
 
153
 
        self.test_func_names[test_case_class] = test_fn_names
 
157
 
class FilteredByModuleTestLoader(TestLoader):
 
158
 
    """A test loader that import only the needed modules."""
 
160
 
    def __init__(self, needs_module):
 
163
 
        :param needs_module: a callable taking a module name as a
 
164
 
            parameter returing True if the module should be loaded.
 
166
 
        TestLoader.__init__(self)
 
167
 
        self.needs_module = needs_module
 
169
 
    def loadTestsFromModuleName(self, name):
 
170
 
        if self.needs_module(name):
 
171
 
            return TestLoader.loadTestsFromModuleName(self, name)
 
173
 
            return self.suiteClass()
 
176
 
def _load_module_by_name(mod_name):
 
177
 
    parts = mod_name.split('.')
 
178
 
    module = __import__(mod_name)
 
180
 
    # for historical reasons python returns the top-level module even though
 
181
 
    # it loads the submodule; we need to walk down to get the one we want.
 
183
 
        module = getattr(module, parts.pop(0))
 
187
 
class TestVisitor(object):
 
188
 
    """A visitor for Tests"""
 
189
 
    def visitSuite(self, aTestSuite):
 
191
 
    def visitCase(self, aTestCase):