汇总PHPUnit的多次执行的代码覆盖率 [英] Aggregating code coverage from several executions of PHPUnit
问题描述
我已经使用PHPUnit已有一段时间了,看来我可能需要将测试分为几个组,分别作为phpunit
的单独执行运行.造成这种情况的主要原因是,我的大多数测试都需要在单独的进程中运行,而有些实际却不能在单独的进程中运行,这是因为记录在文档中的问题
I have been working with PHPUnit for a little while now, and it's starting to look like I may need to break my tests up into groups that would run as separate executions of phpunit
. The main reason for this is that most of my tests need to run in separate processes, while some actually cannot be run in separate processes because of an issue documented here. What I would like to do is write a bash script that fires off several executions of phpunit
, each configured to run different tests with different settings.
所以我的问题是:有没有一种方法可以汇总多个phpunit
执行的代码覆盖率结果?我可以直接通过PHPUnit本身还是使用其他工具来做到这一点?是否可以使用PHPUnit的测试套件概念从一次phpunit
运行中得到我想要的东西?
So my question is: is there a way to aggregate the code coverage results of multiple phpunit
executions? Could I do that directly through PHPUnit itself, or using some other tool? Is it possible to get what I'm looking for out of one run of phpunit
using PHPUnit's test suite concept?
推荐答案
在PHPUnit中使用"--coverage-php
"选项,使其以序列化的PHP_CodeCoverage
对象的形式写入coverage数据,然后使用PHP_CodeCoverage::merge
进行合并,就像这样:
Use the "--coverage-php
" option to PHPUnit to get it to write the coverage data as a serialized PHP_CodeCoverage
object, then combine them using PHP_CodeCoverage::merge
, like this:
<?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
You may also be able to merge the files using a tool named phpcov
, as described here: https://github.com/sebastianbergmann/phpunit/pull/685
这篇关于汇总PHPUnit的多次执行的代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!