227
223
def tearDown(self):
229
225
os.chdir(self.TEST_ROOT)
230
super(InTempDir, self).tearDown()
233
class _MyResult(unittest._TextTestResult):
231
class _MyResult(TestResult):
235
233
Custom TestResult.
237
235
No special behaviour for now.
239
237
def __init__(self, out, style):
240
super(_MyResult, self).__init__(out, False, 0)
239
TestResult.__init__(self)
242
240
assert style in ('none', 'progress', 'verbose')
243
241
self.style = style
245
244
def startTest(self, test):
246
super(_MyResult, self).startTest(test)
247
245
# TODO: Maybe show test.shortDescription somewhere?
249
247
# python2.3 has the bad habit of just "runit" for doctests
250
248
if what == 'runit':
251
249
what = test.shortDescription()
252
251
if self.style == 'verbose':
253
252
print >>self.out, '%-60.60s' % what,
254
elif self.style == 'progress':
257
TestResult.startTest(self, test)
260
def stopTest(self, test):
262
TestResult.stopTest(self, test)
256
265
def addError(self, test, err):
257
266
if self.style == 'verbose':
258
267
print >>self.out, 'ERROR'
259
elif self.style == 'progress':
260
self.stream.write('E')
262
super(_MyResult, self).addError(test, err)
268
TestResult.addError(self, test, err)
269
_show_test_failure('error', test, err, self.out)
264
271
def addFailure(self, test, err):
265
272
if self.style == 'verbose':
266
273
print >>self.out, 'FAILURE'
267
elif self.style == 'progress':
268
self.stream.write('F')
270
super(_MyResult, self).addFailure(test, err)
274
TestResult.addFailure(self, test, err)
275
_show_test_failure('failure', test, err, self.out)
272
277
def addSuccess(self, test):
273
278
if self.style == 'verbose':
274
279
print >>self.out, 'OK'
275
elif self.style == 'progress':
276
self.stream.write('~')
278
super(_MyResult, self).addSuccess(test)
280
def printErrors(self):
281
if self.style == 'progress':
282
self.stream.writeln()
283
super(_MyResult, self).printErrors()
285
def printErrorList(self, flavour, errors):
286
for test, err in errors:
287
self.stream.writeln(self.separator1)
288
self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
289
self.stream.writeln(self.separator2)
290
self.stream.writeln("%s" % err)
291
if isinstance(test, TestCase):
292
self.stream.writeln()
293
self.stream.writeln('log from this test:')
294
print >>self.stream, test._log_buf
297
class TestSuite(unittest.TestSuite):
299
def __init__(self, tests=(), name='test'):
300
super(TestSuite, self).__init__(tests)
303
def run(self, result):
308
self._setup_test_log()
309
self._setup_test_dir()
312
return super(TestSuite,self).run(result)
314
def _setup_test_log(self):
318
log_filename = os.path.abspath(self._name + '.log')
320
TestCase.TEST_LOG = open(log_filename, 'wt', buffering=1)
322
print >>TestCase.TEST_LOG, "tests run at " + time.ctime()
323
print '%-30s %s' % ('test log', log_filename)
325
def _setup_test_dir(self):
329
TestCase.ORIG_DIR = os.getcwdu()
330
TestCase.TEST_ROOT = os.path.abspath(self._name + '.tmp')
332
print '%-30s %s' % ('running tests in', TestCase.TEST_ROOT)
334
if os.path.exists(TestCase.TEST_ROOT):
335
shutil.rmtree(TestCase.TEST_ROOT)
336
os.mkdir(TestCase.TEST_ROOT)
337
os.chdir(TestCase.TEST_ROOT)
339
# make a fake bzr directory there to prevent any tests propagating
340
# up onto the source directory's real branch
341
os.mkdir(os.path.join(TestCase.TEST_ROOT, '.bzr'))
344
class TextTestRunner(unittest.TextTestRunner):
346
def __init__(self, stream=sys.stderr, descriptions=1, verbosity=0, style='progress'):
347
super(TextTestRunner, self).__init__(stream, descriptions, verbosity)
350
def _makeResult(self):
351
return _MyResult(self.stream, self.style)
353
# If we want the old 4 line summary output (count, 0 failures, 0 errors)
354
# we can override run() too.
357
def run_suite(a_suite, name='test', verbose=False):
358
suite = TestSuite((a_suite,),name)
363
runner = TextTestRunner(stream=sys.stdout, style=style)
364
result = runner.run(suite)
280
TestResult.addSuccess(self, test)
284
def run_suite(suite, name='test', verbose=False):
290
_setup_test_log(name)
291
_setup_test_dir(name)
294
# save stdout & stderr so there's no leakage from code-under-test
295
real_stdout = sys.stdout
296
real_stderr = sys.stderr
297
sys.stdout = sys.stderr = TestBase.TEST_LOG
303
result = _MyResult(real_stdout, style)
306
sys.stdout = real_stdout
307
sys.stderr = real_stderr
309
_show_results(result)
365
311
return result.wasSuccessful()
315
def _setup_test_log(name):
319
log_filename = os.path.abspath(name + '.log')
320
TestBase.TEST_LOG = open(log_filename, 'wt', buffering=1) # line buffered
322
print >>TestBase.TEST_LOG, "tests run at " + time.ctime()
323
print '%-30s %s' % ('test log', log_filename)
326
def _setup_test_dir(name):
330
TestBase.ORIG_DIR = os.getcwdu()
331
TestBase.TEST_ROOT = os.path.abspath(name + '.tmp')
333
print '%-30s %s' % ('running tests in', TestBase.TEST_ROOT)
335
if os.path.exists(TestBase.TEST_ROOT):
336
shutil.rmtree(TestBase.TEST_ROOT)
337
os.mkdir(TestBase.TEST_ROOT)
338
os.chdir(TestBase.TEST_ROOT)
340
# make a fake bzr directory there to prevent any tests propagating
341
# up onto the source directory's real branch
342
os.mkdir(os.path.join(TestBase.TEST_ROOT, '.bzr'))
346
def _show_results(result):
348
print '%4d tests run' % result.testsRun
349
print '%4d errors' % len(result.errors)
350
print '%4d failures' % len(result.failures)
354
def _show_test_failure(kind, case, exc_info, out):
355
from traceback import print_exception
358
print >>out, '-' * 60
361
desc = case.shortDescription()
363
print >>out, ' (%s)' % desc
365
print_exception(exc_info[0], exc_info[1], exc_info[2], None, out)
367
if isinstance(case, TestBase):
369
print >>out, 'log from this test:'
370
print >>out, case._log_buf
372
print >>out, '-' * 60