Skip to content

Instantly share code, notes, and snippets.

@albertpark
Last active November 11, 2015 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albertpark/4551957f2332def300f3 to your computer and use it in GitHub Desktop.
Save albertpark/4551957f2332def300f3 to your computer and use it in GitHub Desktop.
Parse JUnit xml log generated by PHPUnit (phpunit ---log-junit build/logs/junit.xml) and print elapsed time each test case. If you want to color time at certain threshold time, get ColorCLI class (see https://gist.github.com/albertpark/985d39b759f356cdbbeb).
<?php
function run($path) {
$xml = simplexml_load_file($path);
$project = $xml->testsuite;
echo sprintf("total: %s msec", formatMsec($project['time'])) . PHP_EOL;
foreach ($project->testsuite as $testsuite) {
echo sprintf(" suite: %s msec : %s", formatMsec($testsuite['time']), $testsuite['name']) . PHP_EOL;
foreach ($testsuite->testcase as $testcase) {
echo sprintf(" case: %s msec : %s", printMsec($testcase['time']), $testcase['name']) . PHP_EOL;
}
}
return 0;
}
function msec($str) {
return floatval((string) $str) * 1000;
}
function formatMsec($time) {
return sprintf('%9.3f', msec($time));
}
function printMsec($time, $warn = 5, $error = 10) {
$str = formatMsec($time);
if (!class_exists('ColorCLI')) {
return $str;
}
$msec = msec($time);
if ($msec < $warn) {
return ColorCLI::lightGreen($str);
} elseif ($msec < $error) {
return ColorCLI::yellow($str);
}
return ColorCLI::red($str);
}
$colorCli = realpath(__DIR__ . '/ColorCLI.php');
if (file_exists($colorCli)) {
include_once $colorCli;
}
$xmlFileName = "junit.xml";
$root = realpath(__DIR__ . "/..");
$path = realpath("$root/build/logs/$xmlFileName");
if ($path === false || !file_exists($path)) {
die("Not found $xmlFileName");
}
return run($path);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment