Skip to content

Instantly share code, notes, and snippets.

@elazar
Created May 25, 2012 23:43
Show Gist options
  • Save elazar/2791232 to your computer and use it in GitHub Desktop.
Save elazar/2791232 to your computer and use it in GitHub Desktop.
PHPUnit --log-junit processor
<?php
/**
* This script is used to process the output of a PHPUnit test run
* that uses --log-junit for the purposes of timing each executed
* test method. It outputs test methods with their respective
* runtimes in order from largest to smallest. Times are in
* seconds. The script accepts paths to any number of files in the
* JUnit log format.
*
* Ex: php phpunit-log-junit.php /path/to/junit-log1.xml /path/to/junit-log2.xml ...
*/
array_shift($argv);
$data = array();
foreach ($argv as $file) {
$doc = new DOMDocument;
$doc->load($file);
$xpath = new DOMXPath($doc);
$results = $xpath->query('//testcase[@class]');
foreach ($results as $result) {
$class = $result->getAttribute('class');
$method = $result->getAttribute('name');
$time = $result->getAttribute('time');
$data[$class . '->' . $method] = $time;
}
$results = $xpath->query('//testsuite/testsuite/testcase');
foreach ($results as $result) {
$class = $result->parentNode->parentNode->getAttribute('name');
$method = $result->getAttribute('name');
$time = $result->getAttribute('time');
$data[$class . '->' . $method] = $time;
}
}
arsort($data);
$pad = strlen(max($data));
foreach ($data as $test => $time) {
echo str_pad($time, $pad, STR_PAD_LEFT), ' ', $test, PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment