Ant を使用して Emma コード カバレッジ レポートを生成するにはどうすればよいですか?
-
09-06-2019 - |
質問
生成する Ant タスクを設定するにはどうすればよいですか エマ コードカバレッジレポート?
解決
ソース ディレクトリとインストルメントされたディレクトリの場所に関する質問に答えるには、次のようにします (これらは、標準のディレクトリ構造に切り替えることができます)。
<property file="build.properties" />
<property name="source" location="src/main/java" />
<property name="test.source" location="src/test/java" />
<property name="target.dir" location="target" />
<property name="target" location="${target.dir}/classes" />
<property name="test.target" location="${target.dir}/test-classes" />
<property name="instr.target" location="${target.dir}/instr-classes" />
クラスパス:
<path id="compile.classpath">
<fileset dir="lib/main">
<include name="*.jar" />
</fileset>
</path>
<path id="test.compile.classpath">
<path refid="compile.classpath" />
<pathelement location="lib/test/junit-4.6.jar" />
<pathelement location="${target}" />
</path>
<path id="junit.classpath">
<path refid="test.compile.classpath" />
<pathelement location="${test.target}" />
</path>
まず、Ant が Emma ライブラリを見つけられる場所を設定する必要があります。
<path id="emma.lib" >
<pathelement location="${emma.dir}/emma.jar" />
<pathelement location="${emma.dir}/emma_ant.jar" />
</path>
次に、タスクをインポートします。
<taskdef resource="emma_ant.properties" classpathref="emma.lib" />
次に、コードをインストルメント化します。
<target name="coverage.instrumentation">
<mkdir dir="${instr.target}"/>
<mkdir dir="${coverage}"/>
<emma>
<instr instrpath="${target}" destdir="${instr.target}" metadatafile="${coverage}/metadata.emma" mode="copy">
<filter excludes="*Test*"/>
</instr>
</emma>
<!-- Update the that will run the instrumented code -->
<path id="test.classpath">
<pathelement location="${instr.target}"/>
<path refid="junit.classpath"/>
<pathelement location="${emma.dir}/emma.jar"/>
</path>
</target>
次に、次のような適切な VM 引数を指定してターゲットを実行します。
<jvmarg value="-Demma.coverage.out.file=${coverage}/coverage.emma" />
<jvmarg value="-Demma.coverage.out.merge=true" />
最後にレポートを生成します。
<target name="coverage.report" depends="coverage.instrumentation">
<emma>
<report sourcepath="${source}" depth="method">
<fileset dir="${coverage}" >
<include name="*.emma" />
</fileset>
<html outfile="${coverage}/coverage.html" />
</report>
</emma>
</target>
他のヒント
の ユーザーガイドには、ビルドスクリプトの設定方法の良い例が記載されています これにより、インストルメント化されたコードを実行から分離するだけでなく、すべてが同じコードに含まれるようになります。 <target>
そのため、一連の異なるターゲットを実行する必要はなく、代わりに次のようなことを行うだけで済みます。 ant emma tests
(もし ant tests
たとえば、これが通常の単体テストの実行方法でした)。
彼らの例は次のとおりです。
<target name="emma" description="turns on EMMA instrumentation/reporting" >
<property name="emma.enabled" value="true" />
<!-- EMMA instr class output directory: -->
<property name="out.instr.dir" value="${basedir}/outinstr" />
<mkdir dir="${out.instr.dir}" />
</target>
<target name="run" depends="init, compile" description="runs the examples" >
<emma enabled="${emma.enabled}" >
<instr instrpathref="run.classpath"
destdir="${out.instr.dir}"
metadatafile="${coverage.dir}/metadata.emma"
merge="true"
/>
</emma>
<!-- note from matt b: you could just as easily have a <junit> task here! -->
<java classname="Main" fork="true" >
<classpath>
<pathelement location="${out.instr.dir}" />
<path refid="run.classpath" />
<path refid="emma.lib" />
</classpath>
<jvmarg value="-Demma.coverage.out.file=${coverage.dir}/coverage.emma" />
<jvmarg value="-Demma.coverage.out.merge=true" />
</java>
<emma enabled="${emma.enabled}" >
<report sourcepath="${src.dir}" >
<fileset dir="${coverage.dir}" >
<include name="*.emma" />
</fileset>
<txt outfile="${coverage.dir}/coverage.txt" />
<html outfile="${coverage.dir}/coverage.html" />
</report>
</emma>
</target>
Emma 2.1 では、ランタイム カバレッジ情報 (.ec ファイル) を取得する別の方法が導入されています。インストルメントされたアプリケーションが実行されているコンピュータの指定されたポートからデータをリモートで要求できます。したがって、VM を停止する必要はありません。
実行時カバレッジ データを含むファイルを取得するには、テストの実行とカバレッジ レポートの生成の間に、Ant スクリプトに次のスニペットを挿入する必要があります。
<emma>
<ctl connect="${emma.rt.host}:${emma.rt.port}" >
<command name="coverage.get" args="${emma.ec.file}" />
<command name="coverage.reset" />
</ctl>
</emma>
他の手順は Emma 2.0 と同様です。それらは完全に説明されています 前の投稿
Emma 2.1 の機能の詳細: http://sourceforge.net/project/shownotes.php?group_id=108932&release_id=336859