【问题标题】:Aggregating code coverage from several executions of PHPUnit从 PHPUnit 的几次执行中聚合代码覆盖率
【发布时间】:2012-04-27 09:47:40
【问题描述】:

我使用 PHPUnit 已经有一段时间了,看起来我可能需要将我的测试分成组,这些组将作为 phpunit 的单独执行运行。主要原因是我的大多数测试需要在单独的进程中运行,而由于here 记录的问题,有些实际上不能在单独的进程中运行。我想做的是编写一个 bash 脚本,该脚本会触发 phpunit 的多次执行,每个执行都配置为使用不同的设置运行不同的测试。

所以我的问题是:有没有办法聚合多个phpunit 执行的代码覆盖率结果?我可以直接通过 PHPUnit 本身或使用其他工具来做到这一点吗?是否有可能使用 PHPUnit 的测试套件概念从 phpunit 的一次运行中得到我正在寻找的东西?

【问题讨论】:

标签: php unit-testing phpunit code-coverage


【解决方案1】:

使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-04
    • 2012-08-04
    • 2019-08-13
    • 2018-05-29
    • 2011-12-17
    • 2017-09-01
    • 2012-01-18
    • 2012-01-14
    相关资源
    最近更新 更多