LoginSignup
11
18

More than 5 years have passed since last update.

MavenでJUnitテストレポートを出力する

Posted at

MavenでJUnitテストを実行して、テストレポート(HTML)を出力する手順をまとめた。

(1) pom.xmlにプラグインの設定を追加する

<project ...>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.1</version>
        <configuration>
          <useSystemClassLoader>false</useSystemClassLoader>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.7.1</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
        <version>2.22.1</version>
      </plugin>
    </plugins>
  </build>
  ...
</project>

maven-surefire-pluginのconfigurationは不具合回避のために必要となる。

参考:https://qiita.com/watanabk/items/16e19e30659d0acca519

その他のプラグインは記載しなくても問題ない。(記載しない場合は最新版が使われる。)

(2) mvnコマンドでテスト実行・レポート生成する

mvn clean \
    test -Dmaven.test.failure.ignore=true \
    site -DgenerateReports=false \
    surefire-report:report

テストが実行されて、テスト結果レポートがtarget/site/surefire-report.htmlに出力される。

各フェーズもしくはゴールの役割は以下の通り。

clean (Phase)

ビルド結果(target)を削除する。

test (Phase)

テストを実行する。失敗したテストがある場合もレポートを出力するために-Dmaven.test.failure.ignore=trueを指定する。

site (Phase)

本来はプロジェクトサイトを出力するためのビルドライフサイクル。surefire-report:reportの実行のみではレポートがリンクする画像とCSSが出力されないため、-DgenerateReports=falseを指定して実行して、画像とCSSのみ出力する。

参考:https://stackoverflow.com/questions/21432663/how-to-get-the-icons-for-the-resulted-maven-surefire-report-plugin

surefire-report:report (Goal)

target/surefire-report内のJUnit実行結果(XML)からレポート(HTML)を出力する。

参考:https://maven.apache.org/surefire/maven-surefire-report-plugin/usage.html

※ 今回はtestフェーズと同時にsurefire-report:reportゴールをしているため問題ないが、surefire-report:reportゴールを単体で実行する際にはtestフェーズが自動的に実行される。レポートのみ出力したい場合にはsurefire-report:report-onlyゴールを指定する。

参考:http://maven.apache.org/surefire/maven-surefire-report-plugin/faq.

11
18
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
11
18