Using Emma for test coverage in Ant
05 Aug 2006 17:05 - (0) comments
Here is how I have used Emma Code Coverage in a project.
<!-- init emma ant task-->
<path id="compile.classpath">
<path refid="emma.lib"/>
</path>
<path id="emma.lib" >
<pathelement location="${dev.lib}/emma.jar" />
<pathelement location="${dev.lib}/emma_ant.jar" />
</path>
<taskdef resource="emma_ant.properties" classpathref="emma.lib" /><!-- Apply emma instrumentation to the classes in instrpath and copy to destdir -->
<target name="emmacompile">
<emma verbosity="info">
<instr instrpath="${build.classes}" destdir="${coverage.classes}" merge="true" mode="copy">
<!-- always exclude every class with a "Test" in the name: -->
<filter excludes="*Test*" />
</instr>
</emma>
</target><path id="test.classpath">
<!-- instrumented classes first -->
<pathelement path="${coverage.classes}" />
<pathelement path="${build.classes}" />
<fileset dir="${app.lib}">
<include name="**/*.jar"/>
</fileset>
<!-- include junit and mock jars-->
<fileset dir="${dev.lib}">
<include name="**/*.jar"/>
</fileset>
</path><target name="test" description="Run all tests and create reports" depends="emmacompile">
<!-- Run tests -->
<junit fork="yes" haltonfailure="no" failureproperty="tests.failed" printsummary="on">
<classpath refid="test.classpath" />
<formatter type="xml"/>
<formatter type="brief" usefile="false"/>
<batchtest fork="yes" todir="${coverage.dir}">
<fileset dir="${test.src}">
<include name="**/*Test.java"/>
</fileset>
</batchtest>
</junit><!-- Generate jUnit report -->
<junitreport todir="./reports">
<fileset dir="${coverage.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="./reports/junit"/>
</junitreport><!-- Move the runtime coverage metadata to the coverage dir -->
<move file="coverage.ec" todir="${coverage.dir}"/>
<move file="coverage.em" todir="${coverage.dir}" failonerror="false"/><!-- Generate code coverage report -->
<emma>
<report sourcepath="${java.src}" depth="method">
<infileset dir="${coverage.dir}" includes="coverage.em,coverage.ec" />
<html outfile="${coverage.dir}/index.html" />
</report>
</emma></target>
If you change some of the settings you could get the following error:
java.lang.IllegalStateException: class [net.deheus.Petrik] appears to be instrumented already
This happens when you use overwrite mode in the instr task and haven't recompiled all classes.
Another problem I encountered was that not all classes were getting covered. Cleaning and recompiling solved this.
Comments
No comments allowed.