LoginSignup
1
2

More than 5 years have passed since last update.

JUnitをバッチ実行してカバレッジも取得する方法

Posted at

はじめに

自分へのメモ的な意味合いも兼ねて書きます。
私はまだSE歴も浅く技術力も知識も未熟なため、間違っているところがあるかもしれません。
もし間違っているところがあれば、指摘していただけると嬉しいです。で、できれば優しく。。

<前提条件>

・Windows10 64bit
・Java7
・eclipse Version: Luna Service Release 2 (4.4.2)
 以下のプラグインがインストール済み
  ・junit ver 4.12
  ・maven 4.0.0
  ・EclEmma 2.3.3
  ・Hudson/Jenkins 1.4.0

1.jacocoをMaven依存関係に追加する。

・pom.xmlファイルに以下の文を追加

<!-- jacoco -->
<dependency>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.7.9</version>
  <scope>test</scope>
</dependency>

2.mvnコマンドを使えるようにする

・まずmavenのインストールパスをシステム環境変数に追加

例)
変数名:M2_HOME
パス :D:\apache-maven-3.3.9

・システム環境のPathに以下を追加する
%M2_HOME%\bin;

3.実行したいプロジェクトディレクトリで以下のコマンドを実行する

mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent test -Dtest=Test.java
mvn org.jacoco:jacoco-maven-plugin:report

<補足>
※ -Dtestは特定のクラスを実行したいときのオプション
※ reportでカバレッジレポートを出力する。
※ レポートのhtmlファイルは実行したディレクトリのtarget/site/jacoco/index.htmlに出力される
※ org.jacoco:jacoco-maven-plugin:prepare-agentのorg.jacocoの部分はpom.xmlの<groupId>に
jacoco-maven-pluginの部分は<artifactId>に対応しているっぽいので、もし

No plugin found for prefix 'jacoco' in the current project and in the plugin groups

↑このエラーが出たらそこを変えてみてください。

◎ 参考サイト

http://qiita.com/umezo@github/items/e750d8e94663f36d9500
https://ishiis.net/2016/10/13/jacoco-coverage/
http://d.hatena.ne.jp/yamap_55/20140727/1406438741
http://wiki.tk2kpdn.com/use-maven-command/
http://d.hatena.ne.jp/joe-hs/20120414/1334388111
http://www.techscore.com/tech/Java/ApacheJakarta/Maven/3/

1
2
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
1
2