プロジェクトテンプレート③
ant/coverage.properties、ant/coverage.xmlはこんな感じ。
build.xmlから呼び出されるAntタスクです。
Coberturaを使って、ユニットテスト実行時のカバレッジを測定します。
カバレッジ測定用のコードをクラスファイルに埋め込んでから、ユニットテストを実行します。
Cobertura:http://cobertura.sourceforge.net/
■ant/coverage.properties
################################################### # カバレッジ作成定義 ################################################### # Coberturaインストールディレクトリ cobertura.home=${basedir}/tools/cobertura-1.9.4.1 # カバレッジデータファイル coverage.data=cobertura.ser # クラスファイル格納ディレクトリ(カバレッジ測定用) instrument.class.dir=instrument # カバレッジ測定結果出力ディレクトリ cobertura.report=coverageReport # カバレッジ測定対象外のクラス instrument.class.dir.excludes=**/*Test.class, **/TestBase.class
■ant/coverage.xml
<?xml version="1.0" encoding="UTF-8"?> <project name="UnitTestCoverage" default="coverage" basedir="."> <!-- ========================================================== --> <!-- 定義値 --> <!-- ========================================================== --> <!-- プロパティファイル名 --> <property file="${basedir}/build.properties" /> <property file="${basedir}/ant/coverage.properties" /> <!-- カバレッジ測定用ライブラリ参照 --> <path id="cobertura.classpath"> <fileset dir="${cobertura.home}"> <include name="cobertura.jar" /> <include name="${lib.dir}/**/*.jar" /> </fileset> </path> <!-- ========================================================== --> <!-- Antタスク定義 --> <!-- ========================================================== --> <taskdef classpathref="cobertura.classpath" resource="tasks.properties" /> <!-- ========================================================== --> <!-- Antタスク実行順序 --> <!-- ========================================================== --> <target name="coverage"> <antcall target="clean" /> <antcall target="instrument" /> <antcall target="unitTest"> <reference refid="build.classpath" torefid="build.classpath"/> </antcall> <antcall target="coverageReport" /> </target> <!-- ========================================================== --> <!-- ディレクトリをクリア --> <!-- ========================================================== --> <target name="clean"> <delete dir="${instrument.class.dir}" /> <delete dir="${cobertura.report}" /> <delete file="${coverage.data}" /> <mkdir dir="${instrument.class.dir}" /> <mkdir dir="${cobertura.report}" /> </target> <!-- ========================================================== --> <!-- カバレッジ測定用コードの埋め込み --> <!-- ========================================================== --> <target name="instrument"> <cobertura-instrument todir="${instrument.class.dir}"> <fileset dir="${class.dir}" excludes="${instrument.class.dir.excludes}" /> </cobertura-instrument> </target> <!-- ========================================================== --> <!-- ユニットテスト実行 --> <!-- ========================================================== --> <target name="unitTest"> <junit fork="yes" maxmemory="512m" printsummary="on"> <classpath refid="cobertura.classpath" /> <classpath location="${instrument.class.dir}" /> <classpath location="${class.dir}" /> <classpath refid="build.classpath" /> <formatter type="xml" /> <batchtest fork="yes" todir="${report.dir}"> <fileset dir="${test.dir}" excludes="${unitTest.test.dir.exclude}" /> </batchtest> </junit> </target> <!-- ========================================================== --> <!-- カバレッジ測定結果を出力 --> <!-- ========================================================== --> <target name="coverageReport"> <cobertura-report format="xml" destdir="${cobertura.report}"> <fileset dir="${src.dir}" includes="**/*.java" /> </cobertura-report> </target> </project>
Jenkinsの設定はこんな感じ。
プロジェクトの設定でAntの呼び出しを変更します。(赤枠の部分です)
targetをbuild-unitTestからbuild-unitTest-coverageに変更します。
ジョブの設定でCoberturaカバレッジ・レポートをチェック
Coberturaの出力結果ファイルを指定します。(赤枠の部分です)