Last active
October 4, 2022 06:09
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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