~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-11 08:06:40 UTC
  • mfrom: (1185.1.58)
  • Revision ID: robertc@lifelesslap.robertcollins.net-20051011080640-7fa85c53602e1ae4
merge in test suite improvements from test-fixes branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import unittest
21
21
import tempfile
22
22
import os
23
 
import sys
24
23
import errno
 
24
import re
 
25
import shutil
25
26
import subprocess
26
 
import shutil
27
 
import re
 
27
import sys
 
28
import time
28
29
 
29
30
import bzrlib.commands
30
31
import bzrlib.trace
70
71
    No special behaviour for now.
71
72
    """
72
73
 
 
74
    def _elapsedTime(self):
 
75
        return "(Took %.3fs)" % (time.time() - self._start_time)
 
76
 
73
77
    def startTest(self, test):
74
78
        unittest.TestResult.startTest(self, test)
75
79
        # TODO: Maybe show test.shortDescription somewhere?
77
81
        if self.showAll:
78
82
            self.stream.write('%-70.70s' % what)
79
83
        self.stream.flush()
 
84
        self._start_time = time.time()
80
85
 
81
86
    def addError(self, test, err):
82
 
        super(_MyResult, self).addError(test, err)
 
87
        unittest.TestResult.addError(self, test, err)
 
88
        if self.showAll:
 
89
            self.stream.writeln("ERROR %s" % self._elapsedTime())
 
90
        elif self.dots:
 
91
            self.stream.write('E')
83
92
        self.stream.flush()
84
93
 
85
94
    def addFailure(self, test, err):
86
 
        super(_MyResult, self).addFailure(test, err)
 
95
        unittest.TestResult.addFailure(self, test, err)
 
96
        if self.showAll:
 
97
            self.stream.writeln("FAIL %s" % self._elapsedTime())
 
98
        elif self.dots:
 
99
            self.stream.write('F')
87
100
        self.stream.flush()
88
101
 
89
102
    def addSuccess(self, test):
90
103
        if self.showAll:
91
 
            self.stream.writeln('OK')
 
104
            self.stream.writeln('OK %s' % self._elapsedTime())
92
105
        elif self.dots:
93
106
            self.stream.write('~')
94
107
        self.stream.flush()
398
411
    result = TestUtil.TestSuite()
399
412
    filter_re = re.compile(pattern)
400
413
    for test in iter_suite_tests(suite):
401
 
        if filter_re.match(test.id()):
 
414
        if filter_re.search(test.id()):
402
415
            result.addTest(test)
403
416
    return result
404
417
 
405
418
 
406
 
def filter_suite_by_names(suite, wanted_names):
407
 
    """Return a new suite containing only selected tests.
408
 
    
409
 
    Names are considered to match if any name is a substring of the 
410
 
    fully-qualified test id (i.e. the class ."""
411
 
    result = TestSuite()
412
 
    for test in iter_suite_tests(suite):
413
 
        this_id = test.id()
414
 
        for p in wanted_names:
415
 
            if this_id.find(p) != -1:
416
 
                result.addTest(test)
417
 
    return result
418
 
 
419
 
 
420
 
def run_suite(suite, name='test', verbose=False, pattern=".*", testnames=None):
 
419
def run_suite(suite, name='test', verbose=False, pattern=".*"):
421
420
    TestCaseInTempDir._TEST_NAME = name
422
421
    if verbose:
423
422
        verbosity = 2
426
425
    runner = TextTestRunner(stream=sys.stdout,
427
426
                            descriptions=0,
428
427
                            verbosity=verbosity)
429
 
    if testnames:
430
 
        suite = filter_suite_by_names(suite, testnames)
431
428
    if pattern != '.*':
432
429
        suite = filter_suite_by_re(suite, pattern)
433
430
    result = runner.run(suite)
442
439
    return result.wasSuccessful()
443
440
 
444
441
 
445
 
def selftest(verbose=False, pattern=".*", testnames=None):
 
442
def selftest(verbose=False, pattern=".*"):
446
443
    """Run the whole test suite under the enhanced runner"""
447
 
    return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern,
448
 
                     testnames=testnames)
 
444
    return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
449
445
 
450
446
 
451
447
def test_suite():