227
216
def tearDown(self):
229
218
os.chdir(self.TEST_ROOT)
230
super(InTempDir, self).tearDown()
233
class _MyResult(unittest._TextTestResult):
224
class _MyResult(TestResult):
235
226
Custom TestResult.
237
228
No special behaviour for now.
239
def __init__(self, out, style):
240
super(_MyResult, self).__init__(out, False, 0)
230
def __init__(self, out):
242
assert style in ('none', 'progress', 'verbose')
232
TestResult.__init__(self)
245
234
def startTest(self, test):
246
super(_MyResult, self).startTest(test)
247
235
# TODO: Maybe show test.shortDescription somewhere?
249
237
# python2.3 has the bad habit of just "runit" for doctests
250
238
if what == 'runit':
251
239
what = test.shortDescription()
252
if self.style == 'verbose':
253
print >>self.out, '%-60.60s' % what,
241
print >>self.out, '%-60.60s' % what,
243
TestResult.startTest(self, test)
245
def stopTest(self, test):
247
TestResult.stopTest(self, test)
256
250
def addError(self, test, err):
257
if self.style == 'verbose':
258
print >>self.out, 'ERROR'
259
elif self.style == 'progress':
260
self.stream.write('E')
262
super(_MyResult, self).addError(test, err)
251
print >>self.out, 'ERROR'
252
TestResult.addError(self, test, err)
253
_show_test_failure('error', test, err, self.out)
264
255
def addFailure(self, test, err):
265
if self.style == 'verbose':
266
print >>self.out, 'FAILURE'
267
elif self.style == 'progress':
268
self.stream.write('F')
270
super(_MyResult, self).addFailure(test, err)
256
print >>self.out, 'FAILURE'
257
TestResult.addFailure(self, test, err)
258
_show_test_failure('failure', test, err, self.out)
272
260
def addSuccess(self, test):
273
if self.style == 'verbose':
274
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)
261
print >>self.out, 'OK'
262
TestResult.addSuccess(self, test)
266
def run_suite(suite, name="test"):
272
_setup_test_log(name)
273
_setup_test_dir(name)
276
# save stdout & stderr so there's no leakage from code-under-test
277
real_stdout = sys.stdout
278
real_stderr = sys.stderr
279
sys.stdout = sys.stderr = TestBase.TEST_LOG
281
result = _MyResult(real_stdout)
284
sys.stdout = real_stdout
285
sys.stderr = real_stderr
287
_show_results(result)
365
289
return result.wasSuccessful()
293
def _setup_test_log(name):
297
log_filename = os.path.abspath(name + '.log')
298
TestBase.TEST_LOG = open(log_filename, 'wt', buffering=1) # line buffered
300
print >>TestBase.TEST_LOG, "tests run at " + time.ctime()
301
print '%-30s %s' % ('test log', log_filename)
304
def _setup_test_dir(name):
308
TestBase.ORIG_DIR = os.getcwdu()
309
TestBase.TEST_ROOT = os.path.abspath(name + '.tmp')
311
print '%-30s %s' % ('running tests in', TestBase.TEST_ROOT)
313
if os.path.exists(TestBase.TEST_ROOT):
314
shutil.rmtree(TestBase.TEST_ROOT)
315
os.mkdir(TestBase.TEST_ROOT)
316
os.chdir(TestBase.TEST_ROOT)
318
# make a fake bzr directory there to prevent any tests propagating
319
# up onto the source directory's real branch
320
os.mkdir(os.path.join(TestBase.TEST_ROOT, '.bzr'))
324
def _show_results(result):
326
print '%4d tests run' % result.testsRun
327
print '%4d errors' % len(result.errors)
328
print '%4d failures' % len(result.failures)
332
def _show_test_failure(kind, case, exc_info, out):
333
from traceback import print_exception
335
print >>out, '-' * 60
338
desc = case.shortDescription()
340
print >>out, ' (%s)' % desc
342
print_exception(exc_info[0], exc_info[1], exc_info[2], None, out)
344
if isinstance(case, TestBase):
346
print >>out, 'log from this test:'
347
print >>out, case._log_buf
349
print >>out, '-' * 60