~bzr-pqm/bzr/bzr.dev

4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2005-2010 Canonical Ltd
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
2
#       Author: Robert Collins <robert.collins@canonical.com>
3
#
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.
8
#
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.
13
#
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
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
17
#
18
19
import sys
20
import logging
21
import unittest
22
1739.1.8 by Robert Collins
Review feedback.
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.
26
__unittest = 1
27
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
28
29
class LogCollector(logging.Handler):
30
    def __init__(self):
31
        logging.Handler.__init__(self)
32
        self.records=[]
33
    def emit(self, record):
34
        self.records.append(record.getMessage())
35
36
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
45
46
47
def visitTests(suite, visitor):
48
    """A foreign method for visiting the tests in a test suite."""
49
    for test in suite._tests:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
50
        #Abusing types to avoid monkey patching unittest.TestCase.
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
51
        # Maybe that would be better?
52
        try:
53
            test.visit(visitor)
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)
60
            else:
61
                print "unvisitable non-unittest.TestCase element %r (%r)" % (test, test.__class__)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
62
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
63
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..."""
68
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)
74
4794.1.5 by Robert Collins
Free tests after executing them as an alternative way to clean up memory usage.
75
    def run(self, result):
76
        """Run the tests in the suite, discarding references after running."""
77
        tests = list(self)
78
        tests.reverse()
79
        self._tests = []
80
        while tests:
81
            if result.shouldStop:
82
                self._tests = reversed(tests)
83
                break
84
            tests.pop().run(result)
85
        return result
86
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
87
88
class TestLoader(unittest.TestLoader):
2921.6.13 by Robert Collins
* Modules can now customise their tests by defining a ``load_tests``
89
    """Custom TestLoader to extend the stock python one."""
90
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
91
    suiteClass = TestSuite
3146.7.2 by Vincent Ladeuil
Review feedback. Fix variable names and scope.
92
    # Memoize test names by test class dict
93
    test_func_names = {}
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
94
1707.2.2 by Robert Collins
Start on bench_add, an add benchtest.
95
    def loadTestsFromModuleNames(self, names):
96
        """use a custom means to load tests from modules.
97
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
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
1707.2.2 by Robert Collins
Start on bench_add, an add benchtest.
100
        requested name is resolvable, if its not raising the original error.
101
        """
102
        result = self.suiteClass()
103
        for name in names:
3302.7.3 by Vincent Ladeuil
Prepare TestLoader for specialization.
104
            result.addTests(self.loadTestsFromModuleName(name))
105
        return result
106
107
    def loadTestsFromModuleName(self, name):
108
        result = self.suiteClass()
109
        module = _load_module_by_name(name)
110
111
        result.addTests(self.loadTestsFromModule(module))
1707.2.2 by Robert Collins
Start on bench_add, an add benchtest.
112
        return result
113
2921.6.13 by Robert Collins
* Modules can now customise their tests by defining a ``load_tests``
114
    def loadTestsFromModule(self, module):
115
        """Load tests from a module object.
116
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.
120
121
        If a load_tests attribute is found, it is called and the result is
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
122
        returned.
2921.6.13 by Robert Collins
* Modules can now customise their tests by defining a ``load_tests``
123
124
        load_tests should be defined like so:
125
        >>> def load_tests(standard_tests, module, loader):
126
        >>>    pass
127
128
        standard_tests is the tests found by the stock TestLoader in the
129
        module, module and loader are the module and loader instances.
130
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])
136
        >>>     return result
137
        """
5340.6.3 by Martin
Less hacky spelling for avoiding unittest native load_tests, as per review by jam
138
        if sys.version_info < (2, 7):
139
            basic_tests = super(TestLoader, self).loadTestsFromModule(module)
140
        else:
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)
2921.6.13 by Robert Collins
* Modules can now customise their tests by defining a ``load_tests``
145
        load_tests = getattr(module, "load_tests", None)
146
        if load_tests is not None:
147
            return load_tests(basic_tests, module, self)
148
        else:
149
            return basic_tests
150
3146.7.2 by Vincent Ladeuil
Review feedback. Fix variable names and scope.
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:
3302.7.8 by Vincent Ladeuil
Fix typos.
154
            # We already know them
3146.7.2 by Vincent Ladeuil
Review feedback. Fix variable names and scope.
155
            return test_fn_names
3146.7.1 by Vincent Ladeuil
Reduce selftest overhead to establish test names by memoization.
156
3146.7.2 by Vincent Ladeuil
Review feedback. Fix variable names and scope.
157
        test_fn_names = unittest.TestLoader.getTestCaseNames(self,
158
                                                             test_case_class)
159
        self.test_func_names[test_case_class] = test_fn_names
160
        return test_fn_names
1707.2.2 by Robert Collins
Start on bench_add, an add benchtest.
161
3302.8.2 by Vincent Ladeuil
New test loader reducing modules imports and tests loaded.
162
163
class FilteredByModuleTestLoader(TestLoader):
164
    """A test loader that import only the needed modules."""
165
166
    def __init__(self, needs_module):
167
        """Constructor.
168
169
        :param needs_module: a callable taking a module name as a
170
            parameter returing True if the module should be loaded.
171
        """
172
        TestLoader.__init__(self)
173
        self.needs_module = needs_module
174
175
    def loadTestsFromModuleName(self, name):
176
        if self.needs_module(name):
177
            return TestLoader.loadTestsFromModuleName(self, name)
178
        else:
179
            return self.suiteClass()
180
181
1707.2.2 by Robert Collins
Start on bench_add, an add benchtest.
182
def _load_module_by_name(mod_name):
183
    parts = mod_name.split('.')
184
    module = __import__(mod_name)
185
    del parts[0]
186
    # for historical reasons python returns the top-level module even though
187
    # it loads the submodule; we need to walk down to get the one we want.
188
    while parts:
189
        module = getattr(module, parts.pop(0))
190
    return module
191
192
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
193
class TestVisitor(object):
194
    """A visitor for Tests"""
195
    def visitSuite(self, aTestSuite):
196
        pass
197
    def visitCase(self, aTestCase):
198
        pass