Changeset 921

Show
Ignore:
Timestamp:
07/22/08 14:57:41 (2 months ago)
Author:
kaste
Message:

Added various filters to the test_runner.

See -? help.
In short you can filter groups, method-names, filenames.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/kaste/PHPUnit_TestSuite/scripts/test_runner.php

    r610 r921  
    99    private $test_suite; 
    1010    private $options = array('verbose'=>false); 
     11    private $filename_filter; 
    1112     
    1213    static function main($args=null) 
     
    5657        while (count($args) > 0){ 
    5758            $arg = array_shift($args); 
    58             if ($this->tryToAddToSuite($this->suite(),$arg)) continue; 
     59            if ($this->addFile($this->suite(),$arg)) continue; 
    5960            else switch ($arg){ 
    6061                case '-v': 
     
    6263                    break; 
    6364                case '-?': 
     65                    $this->drawHelp(); 
     66                    break; 
     67                case '-': 
     68                case '+': 
     69                    $this->addFilter($arg.array_shift($args)); 
     70                    break; 
    6471                default: 
    65                     $this->drawHelp(); 
     72                    $this->addFilter($arg); 
    6673                    break; 
    6774            } 
     
    6976    } 
    7077     
    71     function tryToAddToSuite(PHPUnit_Framework_TestSuite &$suite,$file) 
    72     { 
    73         if (substr($file,0,1)=='_'){ 
    74             return false; 
    75         }elseif (is_file($file)){ 
     78    private function addFilter($arg) 
     79    { 
     80        switch ($arg{0}){ 
     81            case '-': $mode = 'sub'; break; 
     82            case '+': $mode = 'add'; break; 
     83            default: return false;  
     84        } 
     85 
     86        $param = substr($arg,1); 
     87        switch ($this->typeOfFilter($param)){ 
     88            case 'method': 
     89                $pattern = str_replace('*','.*',$param); 
     90                if ($mode=='add'){ 
     91                    $pattern = "/^$pattern/"; 
     92                    $this->options['filter'] = $pattern; 
     93                }else{ 
     94                    //conditional regex-pattern:  
     95                    //if <$pattern> matches, the actual method-name must begin with 'tset' which is always false 
     96                    $pattern = "/^(?(?=$pattern)tset)/"; 
     97                    $this->options['filter'] = $pattern; 
     98                } 
     99                break; 
     100            case 'group': 
     101                $mode == 'add' ? $this->options['groups'][] = $param : $this->options['excludeGroups'][] = $param; 
     102                break; 
     103            case 'filename': 
     104                $pattern = str_replace(array('\\','.','*'),array('\\\\','\.','.*'),$param); 
     105                 
     106                $this->filename_filter = $mode == 'add' ? "/$pattern$/" : "/^(?(?=.*$pattern$).*hph$)/";   
     107                break; 
     108        } 
     109    } 
     110     
     111    private function typeOfFilter($param) 
     112    { 
     113        if (substr($param,0,4)=='test') return 'method'; 
     114        if (substr($param,-4)=='.php')  return 'filename'; 
     115        return 'group'; 
     116    } 
     117     
     118    private function addFile(PHPUnit_Framework_TestSuite &$suite,$file) 
     119    { 
     120        if (is_file($file)){ 
     121            if (!$this->ensureValidFilename((string)$file)) return false; 
    76122            $suite->addTestFile((string)$file,false); 
    77123        }elseif (is_dir($file)){ 
     
    83129    } 
    84130     
     131    private function ensureValidFilename($file) 
     132    { 
     133        if ($file{0}=='_') return false; 
     134         
     135        if (substr($file,-13)=='_TestCase.php'){ 
     136            require_once $file; 
     137            return false; 
     138        } 
     139         
     140        if (substr($file,-4)!='.php') return false; 
     141         
     142        if ($this->filename_filter){ 
     143            if (preg_match($this->filename_filter,$file)) return true; 
     144            return false; 
     145        } 
     146         
     147        return true; 
     148    } 
     149     
    85150    /** 
    86151     * @return PHPUnit_Framework_TestSuite 
     
    91156        $files = new RecursiveDirectoryIterator($path); 
    92157        foreach ($files as $file){ 
    93             $this->tryToAddToSuite($suite,$file); 
     158            $this->addFile($suite,$file); 
    94159        } 
    95160        return $suite; 
     
    106171Usage: 
    107172 
    108 test_runner [-v] [tests/test-suites/folders] 
    109    -v   verbose 
    110    -?   this help 
    111  
    112 This script creates TestSuites on-the fly and runs them. It will exclude filenames or folders which start with an underscore. 
     173test_runner [-v|?] [-|+group] [-|+method] [-|+filename] <filenames|folders> 
     174   -v        verbose 
     175   -?        this help 
     176   -+group   see below 
     177   -+method 
     178   -+file 
     179 
     180This script creates TestSuites on-the fly and runs them.  
     181It will exclude filenames or folders which start with an underscore. As a  
     182convention it will not run filenames which end with <_TestCase.php> but instead 
     183include them. This is so because 'TestCases' use to be abstract classes which  
     184contain common methods or a special Test-Api. 
     185 
     186You can exclude or include groups, methods and/or files. 
     187A minus [-] means 'except', a plus means 'only'. E.g.:  
     188 
     189>  test_runner -v -slow -test*Postgre + DbAdap*.php tests/ 
     190 
     191this will search through all files in the folder <tests/> and its subfolders,  
     192include the one which filename begins with <DbAdap> and exclude the tests which 
     193belong to a group called <slow> or which method name matches <test.*Postgre>. 
     194 
     195So how does this work. We take the sign -|+ to decide if we exclude or include  
     196the following parameter. The parameter gets parsed: if it begins with <test> we 
     197take it as an method-name, if it ends with <.php> we say its a filename,  
     198otherwise it should be a group name. You can use a <*> to match "any character". 
     199You can specify multiple groups, but only one methodname or filename pattern. 
     200 
     201> test_runner tests/ -postgre -sqlite 
     202> test_runner tests/ +mysql 
     203> test_runner tests/ + Request*.php 
     204> test_runner tests/AkRequest/ tests/AkRouter/ 
     205> test_runner tests/ +test*Cast*Null 
    113206 
    114207BANNER;