Changeset 30370

Show
Ignore:
Timestamp:
12/07/05 10:20:49 (3 years ago)
Author:
bdelbosc
Message:

adding --regex to impl #936 TestRunner?: use regexp to load test, adding --list to list tests, make fl-run-test able to lauch standard unittest.TestCase?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • funkload/trunk/funkload/TestRunner.py

    r30289 r30370  
    3131import time 
    3232import unittest 
     33import re 
    3334from optparse import OptionParser, TitledHelpFormatter 
    3435from utils import red_str, green_str, get_version 
     36from funkload.FunkLoadTestCase import FunkLoadTestCase 
    3537 
    3638# ------------------------------------------------------------ 
     
    4143    def loadTestsFromTestCase(self, testCaseClass): 
    4244        """Return a suite of all tests cases contained in testCaseClass""" 
     45        if not issubclass(testCaseClass, FunkLoadTestCase): 
     46            return unittest.TestLoader.loadTestsFromTestCase(self, 
     47                                                             testCaseClass) 
    4348        options = getattr(self, 'options', None) 
    4449        return self.suiteClass([testCaseClass(name, options) for name in 
     
    7984        elif type(obj) == types.UnboundMethodType: 
    8085            # pass funkload options 
    81             return obj.im_class(obj.__name__, self.options) 
     86            if issubclass(obj.im_class, FunkLoadTestCase): 
     87                return obj.im_class(obj.__name__, self.options) 
     88            else: 
     89                return obj.im_class(obj.__name__) 
    8290        elif callable(obj): 
    8391            test = obj() 
     
    155163        return result 
    156164 
     165def filter_testcases(suite, cpattern): 
     166    """Filter a suite with test names that match the compiled regex pattern.""" 
     167    new = unittest.TestSuite() 
     168    for test in suite._tests: 
     169        if isinstance(test, unittest.TestCase): 
     170            name = test.id() # Full test name: package.module.class.method 
     171            name = name[1 + name.rfind('.'):] # extract method name 
     172            if cpattern.search(name): 
     173                new.addTest(test) 
     174        else: 
     175            filtered = filter_testcases(test, cpattern) 
     176            if filtered: 
     177                new.addTest(filtered) 
     178    return new 
     179 
     180 
     181def display_testcases(suite): 
     182    """Display test cases of the suite.""" 
     183    for test in suite._tests: 
     184        if isinstance(test, unittest.TestCase): 
     185            name = test.id() 
     186            name = name[1 + name.find('.'):] 
     187            print name 
     188        else: 
     189            filtered = display_testcases(test) 
     190 
    157191 
    158192class TestProgram(unittest.TestProgram): 
     
    188222                        possible. Output response time stats. You can loop 
    189223                        on many pages using slice -l 2:4. 
     224  %prog myFile.py -e [Ss]ome 
     225                        Run all tests that match the regex [Ss]ome. 
     226  %prog myFile.py --list 
     227                        List all the test names. 
    190228  %prog -h 
    191229                        More options. 
     
    214252            self.module = module 
    215253        self.loadTests() 
    216         self.runTests() 
     254        if self.list_tests: 
     255            display_testcases(self.test) 
     256        else: 
     257            self.runTests() 
    217258 
    218259    def loadTests(self): 
    219260        """Load tests from modules or names.""" 
    220         if self.testNames is None: 
     261        names = self.testNames 
     262        if names is None: 
    221263            self.test = self.testLoader.loadTestsFromModule(self.module) 
    222264        else: 
    223265            self.test = self.testLoader.loadTestsFromNames(self.testNames, 
    224266                                                           self.module) 
     267        if self.test_name_pattern is not None: 
     268            cpattern = re.compile(self.test_name_pattern) 
     269            self.test = filter_testcases(self.test, cpattern) 
    225270 
    226271    def parseArgs(self, argv): 
     
    267312        parser.add_option("--stop-on-fail", action="store_true", 
    268313                          help="Stop tests on first failure or error.") 
     314        parser.add_option("-e", "--regex", type="string", default=None, 
     315                          help="The test names must match the regex.") 
     316        parser.add_option("--list", action="store_true", 
     317                          help="Just list the test names.") 
    269318 
    270319        options, args = parser.parse_args() 
     
    286335            options.ftest_log_to = 'file' 
    287336        self.color = not options.no_color 
     337        self.test_name_pattern = options.regex 
     338        self.list_tests = options.list 
    288339 
    289340        # set testloader options