Laravelのテスト結果をかっこよく!


Laravelのテスト結果をこんな感じに視覚的に見たくて、導入してみました。
走り書きです

環境
Windows10
Laravel 5.6.39

Allure Frameworkのallure2を利用します。

レポートツールの用意

scoop install allure
※Macはbrewで導入できるみたい。

scoopの導入:https://scoop.sh/
allure2のGitHub:https://github.com/allure-framework/allure2

allure-framework/allure-phpunitの導入

$ composer require --dev allure-framework/allure-phpunit
※今回のLaravelのバージョンだと以下のバージョン
composer require --dev allure-framework/allure-phpunit^1.4

レポートツールのPHPUnit用アダプターパッケージ。(ほかの言語も対応しているみたい)

allure-phpunitのGitHub:https://github.com/allure-framework/allure-phpunit

phpunit.xmlに追記

testsuiteタグの次ぐらいにいれておく

phpunit.xml
<listeners>
    <listener class="Yandex\Allure\PhpUnit\AllurePhpUnit" file="vendor/allure-framework/allure-phpunit/src/Yandex/Allure/PhpUnit/AllurePhpUnit.php">
        <arguments>
            <string>build/allure-results</string> <!-- XML files output directory -->
            <boolean>true</boolean> <!-- Whether to delete previous results on rerun -->
            <array> <!-- A list of custom annotations to ignore (optional) -->
                <element key="0">
                    <string>someCustomAnnotation</string>
                </element>
            </array>
        </arguments>
    </listener>
</listeners>

※Duskのxmlにも追記

laravel/build/allure-resultsにレポートファイル(xml等)が出力される
※4行目で変更可能

GitHubの説明:https://github.com/allure-framework/allure-phpunit#installation--usage

結果を視覚化

PowerShell等で、laravel/buildに移動し、

allure generate -cを実行する(-ccleanオプション)

laravel/build/allure-reportに展開される。

結果を見る

展開されたファイルをchromeで開くもCORSに引っかかる。
そのため、CORSを回避するためにchromeのショートカットを作成し、リンク先に以下を追記
--disable-web-security --user-data-dir="C://Chrome dev session"
作成したショートカットのchromeを実行して、build\allure-report\index.htmlをD&D

参考Qiita:https://qiita.com/shika-e/items/808ccdd047133315b95c

テスト実装時

テストを書く際に、レポートで分かりやすくできようなアノテーションも可能で、
タイトル、ディスクリプション、重要度、パラメータ情報、ストーリー、フィーチャー、と色々あるし書いていくほうが良い。

アノテーションの説明:https://github.com/allure-framework/allure-phpunit#main-features


参考リンクまとめ