118
118
def __init__(self, stream, descriptions, verbosity, pb=None):
119
119
unittest._TextTestResult.__init__(self, stream, descriptions, verbosity)
122
def _elapsedTime(self):
123
return "%5dms" % (1000 * (time.time() - self._start_time))
122
def extractBenchmarkTime(self, testCase):
123
"""Add a benchmark time for the current test case."""
124
self._benchmarkTime = getattr(testCase, "_benchtime", None)
126
def _elapsedTestTimeString(self):
127
"""Return a time string for the overall time the current test has taken."""
128
return self._formatTime(time.time() - self._start_time)
130
def _testTimeString(self):
131
if self._benchmarkTime is not None:
133
self._formatTime(self._benchmarkTime),
134
self._elapsedTestTimeString())
136
return " %s" % self._elapsedTestTimeString()
138
def _formatTime(self, seconds):
139
"""Format seconds as milliseconds with leading spaces."""
140
return "%5dms" % (1000 * seconds)
125
142
def _ellipsise_unimportant_words(self, a_string, final_width,
126
143
keep_start=False):
127
"""Add ellipsese (sp?) for overly long strings.
144
"""Add ellipses (sp?) for overly long strings.
129
146
:param keep_start: If true preserve the start of a_string rather
130
147
than the end of it.
168
185
elif self.dots and self.pb is not None:
169
186
self.pb.update(what, self.testsRun - 1, None)
170
187
self.stream.flush()
188
self._recordTestStartTime()
190
def _recordTestStartTime(self):
191
"""Record that a test has started."""
171
192
self._start_time = time.time()
173
194
def addError(self, test, err):
174
195
if isinstance(err[1], TestSkipped):
175
196
return self.addSkipped(test, err)
176
197
unittest.TestResult.addError(self, test, err)
198
self.extractBenchmarkTime(test)
178
self.stream.writeln("ERROR %s" % self._elapsedTime())
200
self.stream.writeln("ERROR %s" % self._testTimeString())
179
201
elif self.dots and self.pb is None:
180
202
self.stream.write('E')
187
209
def addFailure(self, test, err):
188
210
unittest.TestResult.addFailure(self, test, err)
211
self.extractBenchmarkTime(test)
190
self.stream.writeln(" FAIL %s" % self._elapsedTime())
213
self.stream.writeln(" FAIL %s" % self._testTimeString())
191
214
elif self.dots and self.pb is None:
192
215
self.stream.write('F')
199
222
def addSuccess(self, test):
223
self.extractBenchmarkTime(test)
201
self.stream.writeln(' OK %s' % self._elapsedTime())
225
self.stream.writeln(' OK %s' % self._testTimeString())
202
226
elif self.dots and self.pb is None:
203
227
self.stream.write('~')
207
231
unittest.TestResult.addSuccess(self, test)
209
233
def addSkipped(self, test, skip_excinfo):
234
self.extractBenchmarkTime(test)
211
print >>self.stream, ' SKIP %s' % self._elapsedTime()
236
print >>self.stream, ' SKIP %s' % self._testTimeString()
212
237
print >>self.stream, ' %s' % skip_excinfo[1]
213
238
elif self.dots and self.pb is None:
214
239
self.stream.write('S')
515
540
self._runCleanups()
516
541
unittest.TestCase.tearDown(self)
543
def time(self, callable, *args, **kwargs):
544
"""Run callable and accrue the time it takes to the benchmark time."""
545
if self._benchtime is None:
549
callable(*args, **kwargs)
551
self._benchtime += time.time() - start
518
553
def _runCleanups(self):
519
554
"""Run registered cleanup functions.
1019
1054
def selftest(verbose=False, pattern=".*", stop_on_failure=True,
1020
1055
keep_output=False,
1057
test_suite_factory=None):
1022
1058
"""Run the whole test suite under the enhanced runner"""
1023
1059
global default_transport
1024
1060
if transport is None:
1025
1061
transport = default_transport
1026
1062
old_transport = default_transport
1027
1063
default_transport = transport
1028
suite = test_suite()
1065
if test_suite_factory is None:
1066
suite = test_suite()
1068
suite = test_suite_factory()
1030
1069
return run_suite(suite, 'testbzr', verbose=verbose, pattern=pattern,
1031
1070
stop_on_failure=stop_on_failure, keep_output=keep_output,
1032
1071
transport=transport)
1034
1073
default_transport = old_transport
1038
1076
def test_suite():
1039
"""Build and return TestSuite for the whole program."""
1077
"""Build and return TestSuite for the whole of bzrlib.
1079
This function can be replaced if you need to change the default test
1080
suite on a global basis, but it is not encouraged.
1040
1082
from doctest import DocTestSuite
1042
1084
global MODULES_TO_DOCTEST
1115
1157
test_transport_implementations = [
1116
1158
'bzrlib.tests.test_transport_implementations']
1118
TestCase.BZRPATH = osutils.pathjoin(
1119
osutils.realpath(osutils.dirname(bzrlib.__path__[0])), 'bzr')
1120
print '%10s: %s' % ('bzr', osutils.realpath(sys.argv[0]))
1121
print '%10s: %s' % ('bzrlib', bzrlib.__path__[0])
1123
1160
suite = TestSuite()
1124
# python2.4's TestLoader.loadTestsFromNames gives very poor
1125
# errors if it fails to load a named module - no indication of what's
1126
# actually wrong, just "no such module". We should probably override that
1127
# class, but for the moment just load them ourselves. (mbp 20051202)
1128
loader = TestLoader()
1161
loader = TestUtil.TestLoader()
1129
1162
from bzrlib.transport import TransportTestProviderAdapter
1130
1163
adapter = TransportTestProviderAdapter()
1131
1164
adapt_modules(test_transport_implementations, adapter, loader, suite)
1132
for mod_name in testmod_names:
1133
mod = _load_module_by_name(mod_name)
1134
suite.addTest(loader.loadTestsFromModule(mod))
1165
suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
1135
1166
for package in packages_to_test():
1136
1167
suite.addTest(package.test_suite())
1137
1168
for m in MODULES_TO_TEST:
1147
1178
def adapt_modules(mods_list, adapter, loader, suite):
1148
1179
"""Adapt the modules in mods_list using adapter and add to suite."""
1149
for mod_name in mods_list:
1150
mod = _load_module_by_name(mod_name)
1151
for test in iter_suite_tests(loader.loadTestsFromModule(mod)):
1152
suite.addTests(adapter.adapt(test))
1155
def _load_module_by_name(mod_name):
1156
parts = mod_name.split('.')
1157
module = __import__(mod_name)
1159
# for historical reasons python returns the top-level module even though
1160
# it loads the submodule; we need to walk down to get the one we want.
1162
module = getattr(module, parts.pop(0))
1180
for test in iter_suite_tests(loader.loadTestsFromModuleNames(mods_list)):
1181
suite.addTests(adapter.adapt(test))