Skip to content

Instantly share code, notes, and snippets.

@satooshi
Last active October 4, 2022 06:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satooshi/4752226 to your computer and use it in GitHub Desktop.
Save satooshi/4752226 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/satooshi/4750401).
<?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