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