我们的项目是一个J2EE的项目,涉及了spring,hibernate,struts,EJB。部署在websphere上,本地的开发环境是IBM RAD。项目组自己开发了一个基于JUnit的测试框架,可以通过JSP调用测试类得出测试结果。美中不足的是该框架得不到代码覆盖率。于是我们决定引入cobertura.
分析cobertura自带的example的bulid.xml我们可以将其分解成几个步骤:
1.编译源代码,该步骤其实IDE已经替我们完成了,不需要通过ant去编译,所以省去.
Java代码
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="yes">
<classpath refid="cobertura.classpath" />
</javac>
</target>
2.instrument,在这里cobertura生成了instrument后的源代码。并生成cobertura.ser信息文件,该文件很重要,包含了需要被测试的类的信息。
Java代码
<target name="instrument" depends="init,compile">
<!--
Remove the coverage data file and any old instrumentation.
-->
<delete file="cobertura.ser"/>
<!--<delete dir="${instrumented.dir}" />
-->
<!--
Instrument the application classes, writing the
instrumented classes into ${build.instrumented.dir}.
-->
<cobertura-instrument todir="${instrumented.dir}">
<!--
The following line causes instrument to ignore any
source line containing a reference to log4j, for the
purposes of coverage reporting.
-->
<ignore regex="org.apache.log4j.*" />
<fileset dir="${classes.dir}">
<!--
Instrument all the application classes, but
don't instrument the test classes.
-->
<include name="**/*.class" />
<exclude name="**/*Test.class" />
</fileset>
</cobertura-instrument>
</target>