<!-- Coverage 1.7任务--> <property name="cobertura.dir" value="${lib.dir}/three/coverage" /> <path id="cobertura.classpath"> <fileset dir="${cobertura.dir}"> <include name="cobertura.jar" /> <include name="lib/**/*.jar" /> </fileset> </path> <taskdef classpathref="cobertura.classpath" resource="tasks.properties" /> <target name="instrument"> <delete file="cobertura.ser"/> <delete dir="${instrumented.dir}" /> <cobertura-instrument todir="${instrumented.dir}"> <fileset dir="${build.dir.main}"> <include name="**/*.class" /> <exclude name="**/*Test.class" /> </fileset> </cobertura-instrument> </target> <target name="coverage-report"> <cobertura-report destdir="${coverage.out}" srcdir="${src.main}" format="html"/> </target> <target name="Coverage" depends="compile,instrument,junit,coverage-report" /> <target name="test" depends="init,compile"> <junit fork="yes" dir="${basedir}" failureProperty="test.failed"> <!-- Note the classpath order: instrumented classes are before the original (uninstrumented) classes. This is important. --> <classpath location="${instrumented.dir}" /> <classpath location="${classes.dir}" /> <!-- The instrumented classes reference classes used by the Cobertura runtime, so Cobertura and its dependencies must be on your classpath. --> <classpath refid="cobertura_classpath" /> <formatter type="xml" /> <test name="${testcase}" todir="${reports.xml.dir}" if="testcase" /> <batchtest todir="${reports.xml.dir}" unless="testcase"> <fileset dir="${src.dir}"> <include name="**/*Test.java" /> </fileset> </batchtest> </junit> <junitreport todir="${reports.xml.dir}"> <fileset dir="${reports.xml.dir}"> <include name="TEST-*.xml" /> </fileset> <report format="frames" todir="${reports.html.dir}" /> </junitreport> </target>
注意点:
cobertura-1.7 --- jdk1.5
cobertura-1.9.4 --- jdk1.6(应该)
build.dir.main -- java编译后位置
coverage.out -- coverage输出位置
src.main -- 源码位置
三部曲:
instrument junit coverage-report
Junit中的重点
<classpath location="${instrumented.dir}" />
<classpath location="${classes.dir}" />