2012-04-16 134 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