我使用 PHPUnit 已经有一段时间了,看起来我可能需要将我的测试分成多个组,这些组将作为 phpunit
的单独执行运行。这样做的主要原因是我的大部分测试需要在单独的进程中运行,而有些测试实际上不能在单独的进程中运行,因为记录了一个问题 here .我想做的是编写一个 bash 脚本来触发 phpunit
的多次执行,每次执行都配置为使用不同的设置运行不同的测试。
所以我的问题是:有没有办法聚合多个 phpunit
执行的代码覆盖率结果?我可以直接通过 PHPUnit 本身或使用其他工具来做到这一点吗?是否可以使用 PHPUnit 的测试套件概念从一次 phpunit
中获得我正在寻找的东西?
最佳答案
使用 PHPUnit 的 "--coverage-php
"选项让它将覆盖数据写入序列化的 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
关于php - 从 PHPUnit 的几次执行中聚合代码覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10167775/