利用 PHPUnit 9.x 的 extension 特性,可以记录每个测试用例的耗时并存储到 CSV 文件中。Extension 代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <?php declare(strict_types=1); namespace Tests; use PHPUnit\Runner\AfterTestHook; class LongRunningTestsLogger implements AfterTestHook { public function __construct(private string $log_file) { } public function executeAfterTest(string $test, float $time): void { $fp = fopen($this->log_file . '.csv', 'a'); fputcsv($fp, [$test, $time]); fclose($fp); } } phpunit.xml 的配置:1 2 3 4 5 6 7 <extensions> <extension class="Tests\LongRunningTestsLogger"> <arguments> <string>profiling</string> </arguments> </extension> </extensions>