cobertura 在websphere中的使用
关于cobertura的介绍可参考我转载的几篇文章,这里就不做介绍了.这里我想介绍一下cobertura和中间件的结合使用。
我们的项目是一个J2EE的项目,涉及了spring,hibernate,struts,EJB。部署在websphere上,本地的开发环境是IBM RAD。项目组自己开发了一个基于JUnit的测试框架,可以通过JSP调用测试类得出测试结果。美中不足的是该框架得不到代码覆盖率。于是我们决定引入cobertura.
分析cobertura自带的example的bulid.xml我们可以将其分解成几个步骤:
1.编译源代码,该步骤其实IDE已经替我们完成了,不需要通过ant去编译,所以省去.
<target name="compile" depends="init"><javac srcdir="${src.dir}" destdir="${classes.dir}" debug="yes"><classpath refid="cobertura.classpath" /></javac></target><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 theinstrumented classes into ${build.instrumented.dir}.--><cobertura-instrument todir="${instrumented.dir}"><!--The following line causes instrument to ignore anysource line containing a reference to log4j, for thepurposes of coverage reporting.--><ignore regex="org.apache.log4j.*" /><fileset dir="${classes.dir}"><!--Instrument all the application classes, butdon't instrument the test classes.--><include name="**/*.class" /><exclude name="**/*Test.class" /></fileset></cobertura-instrument></target><target name="test" depends="init,compile"><junit fork="yes" dir="${basedir}" failureProperty="test.failed"><!--Note the classpath order: instrumented classes are before theoriginal (uninstrumented) classes. This is important.--><classpath location="${instrumented.dir}" /><classpath location="${classes.dir}" /><!--The instrumented classes reference classes used by theCobertura runtime, so Cobertura and its dependenciesmust 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><target name="coverage-check"><cobertura-check branchrate="34" totallinerate="100" /></target><target name="coverage-report"><!--Generate an XML file containing the coverage data usingthe "srcdir" attribute.--><cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" /></target><target name="alternate-coverage-report"><!--Generate a series of HTML files containing the coveragedata in a user-readable form using nested source filesets.--><cobertura-report destdir="${coverage.html.dir}"><fileset dir="${src.dir}"><include name="**/*.java"/></fileset></cobertura-report></target><target name="merge" description="merge files"><cobertura-merge> <fileset dir="${basedir}"> <include name="**/cobertura1.ser"/> <include name="**/cobertura22.ser"/> </fileset></cobertura-merge></target>