~bzr-pqm/bzr/bzr.dev

5557.1.7 by John Arbash Meinel
Merge in the bzr.dev 5582
1
# Copyright (C) 2005-2011 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
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
23
from bzrlib import pyutils
24
1739.1.8 by Robert Collins
Review feedback.
25
# Mark this python module as being part of the implementation
26
# of unittest: this gives us better tracebacks where the last
27
# shown frame is the test code, not our assertXYZ.
28
__unittest = 1
29
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
30
31
class LogCollector(logging.Handler):
5574.4.2 by Vincent Ladeuil
Fix vertical spaces and lines too long.
32
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
33
    def __init__(self):
34
        logging.Handler.__init__(self)
35
        self.records=[]
5574.4.2 by Vincent Ladeuil
Fix vertical spaces and lines too long.
36
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
37
    def emit(self, record):
38
        self.records.append(record.getMessage())
39
40
41
def makeCollectingLogger():
42
    """I make a logger instance that collects its logs for programmatic analysis
43
    -> (logger, collector)"""
44
    logger=logging.Logger("collector")
45
    handler=LogCollector()
46
    handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
47
    logger.addHandler(handler)
48
    return logger, handler
49
50
51
def visitTests(suite, visitor):
52
    """A foreign method for visiting the tests in a test suite."""
53
    for test in suite._tests:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
54
        #Abusing types to avoid monkey patching unittest.TestCase.
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
55
        # Maybe that would be better?
56
        try:
57
            test.visit(visitor)
58
        except AttributeError:
59
            if isinstance(test, unittest.TestCase):
60
                visitor.visitCase(test)
61
            elif isinstance(test, unittest.TestSuite):
62
                visitor.visitSuite(test)
63
                visitTests(test, visitor)
64
            else:
5574.4.2 by Vincent Ladeuil
Fix vertical spaces and lines too long.
65
                print "unvisitable non-unittest.TestCase element %r (%r)" % (
66
                    test, test.__class__)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
67
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
68
69
class TestSuite(unittest.TestSuite):
70
    """I am an extended TestSuite with a visitor interface.
71
    This is primarily to allow filtering of tests - and suites or
72
    more in the future. An iterator of just tests wouldn't scale..."""
73
74
    def visit(self, visitor):
75
        """visit the composite. Visiting is depth-first.
76
        current callbacks are visitSuite and visitCase."""
77
        visitor.visitSuite(self)
78
        visitTests(self, visitor)
79
4794.1.5 by Robert Collins
Free tests after executing them as an alternative way to clean up memory usage.
80
    def run(self, result):
81
        """Run the tests in the suite, discarding references after running."""
82
        tests = list(self)
83
        tests.reverse()
84
        self._tests = []
85
        while tests:
86
            if result.shouldStop:
87
                self._tests = reversed(tests)
88
                break
5340.14.1 by John Arbash Meinel
Revert out everything that doesn't deal with exc_info changes
89
            tests.pop().run(result)
4794.1.5 by Robert Collins
Free tests after executing them as an alternative way to clean up memory usage.
90
        return result
91
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
92
93
class TestLoader(unittest.TestLoader):
2921.6.13 by Robert Collins
* Modules can now customise their tests by defining a ``load_tests``
94
    """Custom TestLoader to extend the stock python one."""
95
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
96
    suiteClass = TestSuite
3146.7.2 by Vincent Ladeuil
Review feedback. Fix variable names and scope.
97
    # Memoize test names by test class dict
98
    test_func_names = {}
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
99
1707.2.2 by Robert Collins
Start on bench_add, an add benchtest.
100
    def loadTestsFromModuleNames(self, names):
101
        """use a custom means to load tests from modules.
102
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
103
        There is an undesirable glitch in the python TestLoader where a
104
        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.
105
        requested name is resolvable, if its not raising the original error.
106
        """
107
        result = self.suiteClass()
108
        for name in names:
3302.7.3 by Vincent Ladeuil
Prepare TestLoader for specialization.
109
            result.addTests(self.loadTestsFromModuleName(name))
110
        return result
111
112
    def loadTestsFromModuleName(self, name):
113
        result = self.suiteClass()
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
114
        module = pyutils.get_named_object(name)
3302.7.3 by Vincent Ladeuil
Prepare TestLoader for specialization.
115
116
        result.addTests(self.loadTestsFromModule(module))
1707.2.2 by Robert Collins
Start on bench_add, an add benchtest.
117
        return result
118
2921.6.13 by Robert Collins
* Modules can now customise their tests by defining a ``load_tests``
119
    def loadTestsFromModule(self, module):
120
        """Load tests from a module object.
121
122
        This extension of the python test loader looks for an attribute
123
        load_tests in the module object, and if not found falls back to the
124
        regular python loadTestsFromModule.
125
126
        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
127
        returned.
2921.6.13 by Robert Collins
* Modules can now customise their tests by defining a ``load_tests``
128
129
        load_tests should be defined like so:
130
        >>> def load_tests(standard_tests, module, loader):
131
        >>>    pass
132
133
        standard_tests is the tests found by the stock TestLoader in the
134
        module, module and loader are the module and loader instances.
135
136
        For instance, to run every test twice, you might do:
137
        >>> def load_tests(standard_tests, module, loader):
138
        >>>     result = loader.suiteClass()
139
        >>>     for test in iter_suite_tests(standard_tests):
140
        >>>         result.addTests([test, test])
141
        >>>     return result
142
        """
5340.6.3 by Martin
Less hacky spelling for avoiding unittest native load_tests, as per review by jam
143
        if sys.version_info < (2, 7):
144
            basic_tests = super(TestLoader, self).loadTestsFromModule(module)
145
        else:
146
            # GZ 2010-07-19: Python 2.7 unittest also uses load_tests but with
147
            #                a different and incompatible signature
148
            basic_tests = super(TestLoader, self).loadTestsFromModule(module,
149
                use_load_tests=False)
2921.6.13 by Robert Collins
* Modules can now customise their tests by defining a ``load_tests``
150
        load_tests = getattr(module, "load_tests", None)
151
        if load_tests is not None:
152
            return load_tests(basic_tests, module, self)
153
        else:
154
            return basic_tests
155
3146.7.2 by Vincent Ladeuil
Review feedback. Fix variable names and scope.
156
    def getTestCaseNames(self, test_case_class):
157
        test_fn_names = self.test_func_names.get(test_case_class, None)
158
        if test_fn_names is not None:
3302.7.8 by Vincent Ladeuil
Fix typos.
159
            # We already know them
3146.7.2 by Vincent Ladeuil
Review feedback. Fix variable names and scope.
160
            return test_fn_names
3146.7.1 by Vincent Ladeuil
Reduce selftest overhead to establish test names by memoization.
161
3146.7.2 by Vincent Ladeuil
Review feedback. Fix variable names and scope.
162
        test_fn_names = unittest.TestLoader.getTestCaseNames(self,
163
                                                             test_case_class)
164
        self.test_func_names[test_case_class] = test_fn_names
165
        return test_fn_names
1707.2.2 by Robert Collins
Start on bench_add, an add benchtest.
166
3302.8.2 by Vincent Ladeuil
New test loader reducing modules imports and tests loaded.
167
168
class FilteredByModuleTestLoader(TestLoader):
169
    """A test loader that import only the needed modules."""
170
171
    def __init__(self, needs_module):
172
        """Constructor.
173
174
        :param needs_module: a callable taking a module name as a
175
            parameter returing True if the module should be loaded.
176
        """
177
        TestLoader.__init__(self)
178
        self.needs_module = needs_module
179
180
    def loadTestsFromModuleName(self, name):
181
        if self.needs_module(name):
182
            return TestLoader.loadTestsFromModuleName(self, name)
183
        else:
184
            return self.suiteClass()
185
186
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
187
class TestVisitor(object):
188
    """A visitor for Tests"""
5574.4.2 by Vincent Ladeuil
Fix vertical spaces and lines too long.
189
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
190
    def visitSuite(self, aTestSuite):
191
        pass
5574.4.2 by Vincent Ladeuil
Fix vertical spaces and lines too long.
192
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
193
    def visitCase(self, aTestCase):
194
        pass