2011-08-19 18 views
9

Możesz użyć flagi --stop-on-failure, aby przerwać testowanie jednostki, gdy jeden z testów się nie powiedzie.Ponownie uruchom ostatni nieudany test w PHPUnit

Czy jest jakiś sposób na szybkie poinformowanie PHPUnit o ponownym uruchomieniu tego nieudanego testu, zamiast ręcznego podania pełnej ścieżki?

Odpowiedz

9

Spójrz na opcję cli --filter. Możesz znaleźć przykład w organisation docs i CLI Docs.

--filter

Tylko uruchamia testy, których nazwa pasuje do podanego wzorca. Wzorzec może być nazwą pojedynczego testu lub wyrażeniem regularnym pasującym do wielu nazw testowych.

Załóżmy bieg phpunit Tests/ i Tests/Stuff/ThatOneTestClassAgain::testThisWorks zawiedzie:

opcje są:

phpunit --filter ThatOneTestClassAgain

i

phpunit --filter testThisWorks

lub większość innych ciągów że jakoś sensu

+2

Jeśli masz konfigurację phpunit.xml dla swoich badań, stworzyłem skrypt bash, że krótki użyje rejestrowania w phpunit twojego ostatniego uruchomienia testowego, aby zapełnić to - filtr z klasami, które zawierały nieudane testy. Po prostu przeczytaj komentarze na ten temat [gist] (https://gist.github.com/bksunday/6922149#file-test-run-failed). – bksunday

2

Sposób, w jaki udało mi się go wdrożyć, jest dość łatwy, ale wymaga zaimplementowania logowania. Konfigurujesz urządzenie phpunit, aby logować się do pliku json. Następnie należy zmienić komendę phpunit aby coś podobnego do:

cd /home/vagrant/tests && php -d auto_prepend_file=./tests-prepend.php /usr/local/bin/phpunit 

Co to robi to auto_prepend plik php przed wykonaniem PHPUnit. W ten sposób możemy przechwycić argument $ argsv i automatycznie dostarczyć wymagane polecenie filtru do phpunit.

testy-prepend.php (upewnij się, aby zmienić ścieżkę pliku dziennika json)

<?php 

global $argv, $argc; 
if(empty($argv) === false) { 
    // are we re-running? 
    $has_rerun = false; 
    foreach ($argv as $key => $value) { 
     if($value === '--rerun-failures') { 
      $has_rerun = true; 
      unset($argv[$key]); 
      break; 
     } 
    } 
    if($has_rerun === true) { 
     // validate the path exists and if so then capture the json data. 
     $path = realpath(dirname(__FILE__).'/../logs/report.json'); 
     if(is_file($path) === true) { 
      // special consideration taken here as phpunit does not store the report as a json array. 
      $data = json_decode('['.str_replace('}{'.PHP_EOL, '},{'.PHP_EOL, file_get_contents($path).']'), true); 
      $failed = array(); 
      // capture the failures as well as errors but taking care not to capture skipped tests. 
      foreach ($data as $event) { 
       if($event['event'] === 'test') { 
        if($event['status'] === 'fail') { 
         $failed[] = array($event['test'], 'failed'); 
        } 
        elseif($event['status'] === 'error' && $event['trace'][0]['function'] !== 'markTestIncomplete') { 
         $failed[] = array($event['test'], 'error\'d'); 
        } 
       } 
      } 
      if(empty($failed) === true) { 
       echo 'There are no failed tests to re-run.'.PHP_EOL.PHP_EOL; 
       exit; 
      } 
      else{ 
       echo '--------------------------------------------------------------------'.PHP_EOL; 
       echo 'Re-running the following tests: '.PHP_EOL; 
       foreach ($failed as $key => $test_data) { 
        echo ' - '.$test_data[0].' ('.$test_data[1].')'.PHP_EOL; 
        // important to escapre the namespace backslashes. 
        $failed[$key] = addslashes($test_data[0]); 
       } 
       echo '--------------------------------------------------------------------'.PHP_EOL.PHP_EOL; 
      } 
      $argv[] = '--filter'; 
      $argv[] = '/('.implode('|', $failed).')/'; 
      // important to update the globals in every location. 
      $_SERVER['argv'] = $GLOBALS['_SERVER']['argv'] = $GLOBALS['argv'] = $argv = array_values($argv); 
      $_SERVER['argc'] = $GLOBALS['_SERVER']['argc'] = $GLOBALS['argc'] = $argc = count($argv); 
     } 
     else{ 
      echo 'The last run report log at '.$path.' does not exist so it is not possible to re-run the failed tests. Please re-run the test suite without the --rerun-failures command.'.PHP_EOL.PHP_EOL; 
      exit; 
     } 
    } 
} 
Powiązane problemy