2011-08-19 26 views

Trả lời

9

Hãy xem tùy chọn cli --filter. Bạn có thể tìm thấy ví dụ trong số organisation docs và trong số CLI Docs.

--filter

Chỉ chạy thử nghiệm có tên phù hợp với mô hình nhất định. Mẫu có thể là tên của một thử nghiệm đơn lẻ hoặc một cụm từ thông dụng khớp với nhiều tên thử nghiệm.

Giả chạy của bạn phpunit Tests/Tests/Stuff/ThatOneTestClassAgain::testThisWorks thất bại:

lựa chọn của bạn là:

phpunit --filter ThatOneTestClassAgain

phpunit --filter testThisWorks

hoặc hầu hết các chuỗi khác mà bằng cách nào đó có ý nghĩa

+2

Nếu bạn có một thiết lập phpunit.xml cho các bài kiểm tra của bạn, tôi tạo ra một kịch bản bash ngắn sẽ sử dụng đăng nhập phpunit của lần chạy thử cuối cùng của bạn để điền thông tin này --filter với các lớp có chứa các kiểm tra không thành công. Đơn giản chỉ cần đọc các ý kiến ​​trên đầu trang này [gist] (https://gist.github.com/bksunday/6922149#file-test-run-failed). – bksunday

2

Cách tôi đã tìm thấy để triển khai nó khá dễ dàng, nhưng yêu cầu phải đăng nhập để được triển khai. Bạn thiết lập phpunit để đăng nhập vào một tập tin json. Sau đó, bạn thay đổi lệnh phpunit thành một cái gì đó tương tự như:

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

Điều này có nghĩa là auto_prepend một tệp php trước khi thực thi phpunit. Bằng cách này chúng ta có thể bắt $ argsv và cung cấp lệnh lọc yêu cầu tự động cho phpunit.

kiểm tra-prepend.php (hãy chắc chắn để sửa đổi các con đường tập tin của bản ghi 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; 
     } 
    } 
} 
Các vấn đề liên quan