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
1092
1134
'bzrlib.tests.test_sftp_transport',
1093
1135
'bzrlib.tests.test_smart_add',
1094
1136
'bzrlib.tests.test_source',
1137
'bzrlib.tests.test_status',
1095
1138
'bzrlib.tests.test_store',
1096
1139
'bzrlib.tests.test_symbol_versioning',
1097
1140
'bzrlib.tests.test_testament',
1114
1157
test_transport_implementations = [
1115
1158
'bzrlib.tests.test_transport_implementations']
1117
TestCase.BZRPATH = osutils.pathjoin(
1118
osutils.realpath(osutils.dirname(bzrlib.__path__[0])), 'bzr')
1119
print '%10s: %s' % ('bzr', osutils.realpath(sys.argv[0]))
1120
print '%10s: %s' % ('bzrlib', bzrlib.__path__[0])
1122
1160
suite = TestSuite()
1123
# python2.4's TestLoader.loadTestsFromNames gives very poor
1124
# errors if it fails to load a named module - no indication of what's
1125
# actually wrong, just "no such module". We should probably override that
1126
# class, but for the moment just load them ourselves. (mbp 20051202)
1127
loader = TestLoader()
1161
loader = TestUtil.TestLoader()
1128
1162
from bzrlib.transport import TransportTestProviderAdapter
1129
1163
adapter = TransportTestProviderAdapter()
1130
1164
adapt_modules(test_transport_implementations, adapter, loader, suite)
1131
for mod_name in testmod_names:
1132
mod = _load_module_by_name(mod_name)
1133
suite.addTest(loader.loadTestsFromModule(mod))
1165
suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
1134
1166
for package in packages_to_test():
1135
1167
suite.addTest(package.test_suite())
1136
1168
for m in MODULES_TO_TEST:
1146
1178
def adapt_modules(mods_list, adapter, loader, suite):
1147
1179
"""Adapt the modules in mods_list using adapter and add to suite."""
1148
for mod_name in mods_list:
1149
mod = _load_module_by_name(mod_name)
1150
for test in iter_suite_tests(loader.loadTestsFromModule(mod)):
1151
suite.addTests(adapter.adapt(test))
1154
def _load_module_by_name(mod_name):
1155
parts = mod_name.split('.')
1156
module = __import__(mod_name)
1158
# for historical reasons python returns the top-level module even though
1159
# it loads the submodule; we need to walk down to get the one we want.
1161
module = getattr(module, parts.pop(0))
1180
for test in iter_suite_tests(loader.loadTestsFromModuleNames(mods_list)):
1181
suite.addTests(adapter.adapt(test))