18
7

More than 5 years have passed since last update.

PHPUnitの遅いテストケースをあぶり出すphpunit-speedtrap

Posted at

単体テストは数秒で結果が出ると、テストを回しながらの開発がしやすくなったり、継続的インテグレーションの待ち時間が短くなったりと良いことが多い。しかし、プロジェクトが成長するにつれ、テストにかかる時間が伸びだし、待つのが辛くなってくることがある。

johnkary/phpunit-speedtrapはPHPUnitの遅いテストケースを報告してくれるプラグインだ。これを導入すると、実行時間がしきい値を超えたテストケースについて、遅いテストワーストランキングをテスト結果に表示してくれる。その結果をヒントに遅いテストをチューニングすることができる。

687474703a2f2f692e696d6775722e636f6d2f5a7233346769522e706e67.png

インストール方法

composer require --dev johnkary/phpunit-speedtrap

もしも、PHPUnit6系を使っている場合は、最新のバージョン3.0は入らないため、phpunit-speedtrapの2.0を入れる必要がある:

composer require --dev johnkary/phpunit-speedtrap='^2.0'

設定

phpunit.xmlにlistnerを追加するだけ:

phpunit.xml
<phpunit bootstrap="vendor/autoload.php">
...
    <listeners>
        <listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener" />
    </listeners>
</phpunit>

しきい値を設定することもできる:

  • slowThreshold: 「遅いテストケース」とするしきい値。デフォルトは500ミリ秒。
  • reportLength: テスト結果に表示する最大テストケース数。デフォルトは10件。
phpunit.xml
<phpunit bootstrap="vendor/autoload.php">
    <!-- ... other suite configuration here ... -->

    <listeners>
        <listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener">
            <arguments>
                <array>
                    <element key="slowThreshold">
                        <integer>500</integer>
                    </element>
                    <element key="reportLength">
                        <integer>5</integer>
                    </element>
                </array>
            </arguments>
        </listener>
    </listeners>
</phpunit>
18
7
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
18
7