~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/__init__.py

  • Committer: Robert Collins
  • Date: 2005-10-05 05:29:27 UTC
  • mfrom: (1393.1.51)
  • Revision ID: robertc@robertcollins.net-20051005052926-03d3a49e1dbcb670
mergeĀ fromĀ martin

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import errno
25
25
import subprocess
26
26
import shutil
 
27
import re
27
28
 
28
29
import bzrlib.commands
29
30
import bzrlib.trace
112
113
        return EarlyStoppingTestResultAdapter(result)
113
114
 
114
115
 
115
 
class filteringVisitor(TestUtil.TestVisitor):
116
 
    """I accruse all the testCases I visit that pass a regexp filter on id
117
 
    into my suite
118
 
    """
119
 
 
120
 
    def __init__(self, filter):
121
 
        import re
122
 
        TestUtil.TestVisitor.__init__(self)
123
 
        self._suite=None
124
 
        self.filter=re.compile(filter)
125
 
 
126
 
    def suite(self):
127
 
        """answer the suite we are building"""
128
 
        if self._suite is None:
129
 
            self._suite=TestUtil.TestSuite()
130
 
        return self._suite
131
 
 
132
 
    def visitCase(self, aCase):
133
 
        if self.filter.match(aCase.id()):
134
 
            self.suite().addTest(aCase)
 
116
def iter_suite_tests(suite):
 
117
    """Return all tests in a suite, recursing through nested suites"""
 
118
    for item in suite._tests:
 
119
        if isinstance(item, unittest.TestCase):
 
120
            yield item
 
121
        elif isinstance(item, unittest.TestSuite):
 
122
            for r in iter_suite_tests(item):
 
123
                yield r
 
124
        else:
 
125
            raise Exception('unknown object %r inside test suite %r'
 
126
                            % (item, suite))
 
127
 
135
128
 
136
129
class TestSkipped(Exception):
137
130
    """Indicates that a test was intentionally skipped, rather than failing."""
397
390
        ##assert 0
398
391
 
399
392
 
400
 
 
401
 
def run_suite(suite, name='test', verbose=False, pattern=".*"):
 
393
def filter_suite_by_re(suite, pattern):
 
394
    result = TestUtil.TestSuite()
 
395
    filter_re = re.compile(pattern)
 
396
    for test in iter_suite_tests(suite):
 
397
        if filter_re.match(test.id()):
 
398
            result.addTest(test)
 
399
    return result
 
400
 
 
401
 
 
402
def filter_suite_by_names(suite, wanted_names):
 
403
    """Return a new suite containing only selected tests.
 
404
    
 
405
    Names are considered to match if any name is a substring of the 
 
406
    fully-qualified test id (i.e. the class ."""
 
407
    result = TestSuite()
 
408
    for test in iter_suite_tests(suite):
 
409
        this_id = test.id()
 
410
        for p in wanted_names:
 
411
            if this_id.find(p) != -1:
 
412
                result.addTest(test)
 
413
    return result
 
414
 
 
415
 
 
416
def run_suite(suite, name='test', verbose=False, pattern=".*", testnames=None):
402
417
    TestCaseInTempDir._TEST_NAME = name
403
418
    if verbose:
404
419
        verbosity = 2
407
422
    runner = TextTestRunner(stream=sys.stdout,
408
423
                            descriptions=0,
409
424
                            verbosity=verbosity)
410
 
    visitor = filteringVisitor(pattern)
411
 
    suite.visit(visitor)
412
 
    result = runner.run(visitor.suite())
 
425
    if testnames:
 
426
        suite = filter_suite_by_names(suite, testnames)
 
427
    if pattern != '.*':
 
428
        suite = filter_suite_by_re(suite, pattern)
 
429
    result = runner.run(suite)
413
430
    # This is still a little bogus, 
414
431
    # but only a little. Folk not using our testrunner will
415
432
    # have to delete their temp directories themselves.
421
438
    return result.wasSuccessful()
422
439
 
423
440
 
424
 
def selftest(verbose=False, pattern=".*"):
 
441
def selftest(verbose=False, pattern=".*", testnames=None):
425
442
    """Run the whole test suite under the enhanced runner"""
426
 
    return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
 
443
    return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern,
 
444
                     testnames=testnames)
427
445
 
428
446
 
429
447
def test_suite():
464
482
                   'bzrlib.selftest.testtransport',
465
483
                   'bzrlib.selftest.testgraph',
466
484
                   'bzrlib.selftest.testworkingtree',
 
485
                   'bzrlib.selftest.test_upgrade',
467
486
                   ]
468
487
 
469
488
    for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,