LoginSignup
15
13

More than 5 years have passed since last update.

PHPUnitでとにかく楽に遅いテストをピックアップする

Last updated at Posted at 2015-03-19

とりあえず楽したいならこんな感じで PHPUnit の listener を作ればいい。

tests/listeners/PickSlowListener.php
<?php

class PickSlowListener extends PHPUnit_Framework_BaseTestListener
{
    private $test_start_time;

    private $test_time_results = array();

    private $rank_limit;

    public function __construct($rank_limit)
    {
        $this->rank_limit = $rank_limit;
    }

    public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
    {
        $this->test_start_time = microtime(true);
    }

    public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
    {
        $this->test_time_results[$suite->getName()] = microtime(true) - $this->test_start_time;
    }

    public function __destruct()
    {
        if (empty($this->test_time_results)) {
            return;
        }

        echo 'SLOW TEST WORST ' . $this->rank_limit . PHP_EOL;
        arsort($this->test_time_results);
        foreach (array_slice($this->test_time_results, 0, $this->rank_limit) as $test_name => $time) {
            echo $test_name . ':' . $time . PHP_EOL;
        }
    }
}

設定はこんな感じで

diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 858b395..204e559 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -21,4 +21,13 @@
     <logging>
         <log type="coverage-clover" target="build/logs/clover.xml"/>
     </logging>
+
+    <listeners>
+        <listener class="PickSlowListener" file="tests/listeners/PickSlowListener.php">
+            <arguments>
+                <integer name="rank_limit">1</integer>
+            </arguments>
+        </listener>
+    </listeners>
 </phpunit>

出力はこのようになる

 ~/Documents/program/ghq/github.com/gong023/assert_chain/ [master*] ./vendor/bin/phpunit
PHPUnit 4.4.0 by Sebastian Bergmann.

........

Time: 1.48 seconds, Memory: 10.50Mb

OK (8 tests, 69 assertions)

Generating code coverage report in Clover XML format ... done
SLOW TEST WORST 1
AssertChain\Container\Test\AggregaterTest:0.64362788200378
15
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
13