2012-04-16 89 views
22

我一直在使用PHPUnit一段時間,現在看起來好像我可能需要將我的測試分解成可作爲單獨執行的phpunit運行的組。造成這種情況的主要原因是我的大多數測試需要在不同的進程中運行,而有些實際上不能在單獨的進程中運行,因爲存在here文檔記錄的問題。我想要做的是編寫一個bash腳本,觸發多個phpunit的執行,每個執行都配置爲使用不同的設置運行不同的測試。來自PHPUnit的幾次執行的彙總代碼覆蓋率

所以我的問題是:有沒有辦法來聚合多個phpunit執行的代碼覆蓋率結果?我可以直接通過PHPUnit本身或使用其他工具來做到這一點嗎?使用PHPUnit的測試套件概念,可以從phpunit的一次運行中獲得我正在尋找的內容嗎?

+0

您是否在https://github.com/sebastianbergmann/phpunit/issues/254上看到最新評論? – vimdude 2012-04-16 01:49:39

+0

@abdelsaid:我看了最後一條評論,以及它與這個問題的關係超出了我的想象。 – 2012-04-16 05:47:24

回答

21

使用「--coverage-php」選項PHPUnit來得到它的覆蓋數據寫入一個序列化PHP_CodeCoverage對象,然後使用PHP_CodeCoverage::merge將它們組合起來,就像這樣:

<?php 
/** 
* Deserializes PHP_CodeCoverage objects from the files passed on the command line, 
* combines them into a single coverage object and creates an HTML report of the 
* combined coverage. 
*/ 

if ($argc <= 2) { 
    die("Usage: php generate-coverage-report.php cov-file1 cov-file2 ..."); 
} 

// Init the Composer autoloader 
require realpath(dirname(__FILE__)) . '/../vendor/autoload.php'; 

foreach (array_slice($argv, 1) as $filename) { 
    // See PHP_CodeCoverage_Report_PHP::process 
    // @var PHP_CodeCoverage 
    $cov = unserialize(file_get_contents($filename)); 
    if (isset($codeCoverage)) { 
    $codeCoverage->filter()->addFilesToWhitelist($cov->filter()->getWhitelist()); 
    $codeCoverage->merge($cov); 
    } else { 
    $codeCoverage = $cov; 
    } 
} 

print "\nGenerating code coverage report in HTML format ..."; 

// Based on PHPUnit_TextUI_TestRunner::doRun 
$writer = new PHP_CodeCoverage_Report_HTML(
    'UTF-8', 
    false, // 'reportHighlight' 
    35, // 'reportLowUpperBound' 
    70, // 'reportHighLowerBound' 
    sprintf(
    ' and <a href="http://phpunit.de/">PHPUnit %s</a>', 
    PHPUnit_Runner_Version::id() 
    ) 
); 

$writer->process($codeCoverage, 'coverage'); 

print " done\n"; 
print "See coverage/index.html\n"; 

您可能還可以合併文件使用名爲phpcov的工具,如下所述:https://github.com/sebastianbergmann/phpunit/pull/685

+0

我試過這個,但是我得到了這些錯誤信息: – Phoenix 2017-04-19 07:38:17

+0

(!)致命錯誤:未捕獲異常'RuntimeException'帶消息'無法寫入/arroyo/www/htdocs/code_coverage_report/index.dashboard.html。' in /arroyo/www/htdocs/cdsm/tests/library/PHPUnit/Text/Template.php on line 150(!)RuntimeException:無法寫入/arroyo/www/htdocs/code_coverage_report/index.dashboard.html 。在/arroyo/www/htdocs/cdsm/tests/library/PHPUnit/Text/Template.php on line 150 – Phoenix 2017-04-19 07:45:04

相關問題