Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
Did you see the last comment on github.com/sebastianbergmann/phpunit/issues/254 ? –  vimdude Apr 16 '12 at 1:49
    
@abdelsaid: I looked at that last comment, and how it is relevant to this question is beyond me. –  Ira Baxter Apr 16 '12 at 5:47

2 Answers 2

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";

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

share|improve this answer

I don't know how to do that with PHPUnit directly. To the extent that it produces test coverage data as an abstract set of pairs, it should be relatively easy to combine multiple sets of test coverage data in the abstract. Whether it does that, or whether that is easy, is another question.

Our PHP Test Coverage will collect test coverage data for any tests you care to run under any framework (including PHPUnit), and will allow you to easily combine coverage data from separate runs, out of the box. It will even verify when it is safe to do so (e.g., you haen't changed the source files).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.