37
38
from logging import debug, warning, error
42
class EarlyStoppingTestResultAdapter(object):
43
"""An adapter for TestResult to stop at the first first failure or error"""
45
def __init__(self, result):
48
def addError(self, test, err):
49
self._result.addError(test, err)
52
def addFailure(self, test, err):
53
self._result.addFailure(test, err)
56
def __getattr__(self, name):
57
return getattr(self._result, name)
59
def __setattr__(self, name, value):
61
object.__setattr__(self, name, value)
62
return setattr(self._result, name, value)
65
class _MyResult(unittest._TextTestResult):
69
No special behaviour for now.
72
def startTest(self, test):
73
unittest.TestResult.startTest(self, test)
74
# TODO: Maybe show test.shortDescription somewhere?
75
what = test.shortDescription() or test.id()
77
self.stream.write('%-70.70s' % what)
80
def addError(self, test, err):
81
super(_MyResult, self).addError(test, err)
84
def addFailure(self, test, err):
85
super(_MyResult, self).addFailure(test, err)
88
def addSuccess(self, test):
90
self.stream.writeln('OK')
92
self.stream.write('~')
94
unittest.TestResult.addSuccess(self, test)
96
def printErrorList(self, flavour, errors):
97
for test, err in errors:
98
self.stream.writeln(self.separator1)
99
self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
100
if hasattr(test, '_get_log'):
101
self.stream.writeln()
102
self.stream.writeln('log from this test:')
103
print >>self.stream, test._get_log()
104
self.stream.writeln(self.separator2)
105
self.stream.writeln("%s" % err)
108
class TextTestRunner(unittest.TextTestRunner):
110
def _makeResult(self):
111
result = _MyResult(self.stream, self.descriptions, self.verbosity)
112
return EarlyStoppingTestResultAdapter(result)
115
class filteringVisitor(TestUtil.TestVisitor):
116
"""I accruse all the testCases I visit that pass a regexp filter on id
120
def __init__(self, filter):
122
TestUtil.TestVisitor.__init__(self)
124
self.filter=re.compile(filter)
127
"""answer the suite we are building"""
128
if self._suite is None:
129
self._suite=TestUtil.TestSuite()
132
def visitCase(self, aCase):
133
if self.filter.match(aCase.id()):
134
self.suite().addTest(aCase)
136
class TestSkipped(Exception):
137
"""Indicates that a test was intentionally skipped, rather than failing."""
39
141
class CommandFailed(Exception):
401
def run_suite(suite, name='test', verbose=False, pattern=".*"):
402
TestCaseInTempDir._TEST_NAME = name
407
runner = TextTestRunner(stream=sys.stdout,
410
visitor = filteringVisitor(pattern)
412
result = runner.run(visitor.suite())
413
# This is still a little bogus,
414
# but only a little. Folk not using our testrunner will
415
# have to delete their temp directories themselves.
416
if result.wasSuccessful():
417
if TestCaseInTempDir.TEST_ROOT is not None:
418
shutil.rmtree(TestCaseInTempDir.TEST_ROOT)
420
print "Failed tests working directories are in '%s'\n" % TestCaseInTempDir.TEST_ROOT
421
return result.wasSuccessful()
299
424
def selftest(verbose=False, pattern=".*"):
300
425
"""Run the whole test suite under the enhanced runner"""
301
return testsweet.run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
426
return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
304
429
def test_suite():
305
430
"""Build and return TestSuite for the whole program."""
306
from bzrlib.selftest.TestUtil import TestLoader, TestSuite
307
import bzrlib, bzrlib.store, bzrlib.inventory, bzrlib.branch
308
import bzrlib.osutils, bzrlib.commands, bzrlib.merge3, bzrlib.plugin
431
import bzrlib.store, bzrlib.inventory, bzrlib.branch
432
import bzrlib.osutils, bzrlib.merge3, bzrlib.plugin
309
433
from doctest import DocTestSuite
311
435
global MODULES_TO_TEST, MODULES_TO_DOCTEST