くずきのblog

技術とか色々

Pretty Result PrinterでPHPUnitのテストを見やすくする

どうも、くずきです。
今日は、PHPUnitを見やすくるPretty Result Printerを紹介したいと思います。

Pretty Result Printerとは

そもそも今のPHPUnitは結果が見辛かったりします。

PHPUnit 5.7.26 by Sebastian Bergmann and contributors.

.........................................F...........             53 / 53 (100%)

Time: 8.72 seconds, Memory: 32.00MB

There was 1 failure:

1) Tests\Unit\App\Services\HttpClientTest::it_returns_correct_xxxx
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'/api/entries/1'
+'/api/entries/2'

/xxx/tests/Unit/Services/HttpClientTest.php:34

FAILURES!
Tests: 53, Assertions: 125, Failures: 1.

ここらへんを改善するためにできたのがPretty Result Printer

github.com

PHPUnit 5.7.26 by Sebastian Bergmann and contributors.


 ==> ...erFollowerAttributeForDepartmentStoreTest ✓ ✓
 ==> ...ncerFollowerAttributeForDiscountStoreTest ✓ ✓
 ==> ...fluencerFollowerAttributeForDrugstoreTest ✓ ✓
 ==> ...luencerFollowerAttributeForOnlineTest ✓ ✓
 ==> ...uencerFollowerAttributeForVarietyShopTest ✓ ✓
 ==> ...uencerFollowerScoreForDepartmentStoreTest ✓
 ==> ...uencerFollowerScoreForDiscountStoreTest ✓
 ==> ...luencerFollowerScoreForDrugstoreTest ✓
 ==> ...uencerFollowerScoreForOnlineTest ✓
 ==> ...ncerFollowerScoreForVarietyShopTest ✓
 ==> ...ns\Services\FollowerChangeRateServiceTest ✓ ✓ ✓
 ==> ...Domains\Services\FollowerScoreServiceTest ✓ ✓ ✓
 ==> ...it\Domains\ValueObjects\AttributeTypeTest ✓ ✓ ✓ ✓
 ==> ...it\Domains\ValueObjects\FollowerCountTest ✓ ✓ ✓ ✓ ✓
 ==> Tests\Unit\ExampleTest                       ✓
 ==> App\Http\Requests\GuzzleRequestTest          ✓
 ==> Tests\Unit\Models\InstagramInsightTest       ✓ ✓ ✓
 ==> ...s\Unit\App\Services\HttpClientTest ✓ ✖ ✓ ✓ ✓ ✓ ✓ ✓ ✓
 ==> ...Unit\App\Services\ResponseJsonTest ✓ ✓ ✓ ✓

Time: 8.18 seconds, Memory: 32.00MB

There was 1 failure:

1) Tests\Unit\App\Services\HttpClientTest::it_returns_correct_xxx
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'/api/entries/1'
+'/api/entries/2'

/xxxx/tests/Unit/Services/HttpClientTest.php:34

FAILURES!
Tests: 53, Assertions: 125, Failures: 1.

このように、どこのテストがこけたのがざっくりと見やすくなる。

導入と実行

導入は簡単で、Laravelcomposer.jsonに追加

    "require-dev": {
        "barryvdh/laravel-ide-helper": "^2.4",
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~5.7",
        "codedungeon/phpunit-result-printer": "^0.4.4",  // 追加
    },

使用方法は簡単で、phpunit.xmlに書いてもいいけどすぐ試すには

phpunit --printer=Codedungeon\\PHPUnitPrettyResultPrinter\\Printer

でさきほどの結果が得られる。

おまけ

Laravelの開発者であるTaylor Otwellツイッターで次のLaravelのバージョンである5.6に入れるか検討してるみたい。