Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

php - Aggregating code coverage from several executions of 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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 "
Generating 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
";
print "See coverage/index.html
";

You may also be able to merge the files using a tool named phpcov, as described here: https://github.com/sebastianbergmann/phpunit/pull/685


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...