`
liyixing1
  • 浏览: 940351 次
  • 性别: Icon_minigender_1
  • 来自: 江西上饶
社区版块
存档分类
最新评论

cobertura做测试覆盖率 ant的配置

 
阅读更多
cobertura原理大致如下:
首先修改我们编译后的class文件,再代码里面加入cobertura的统计代码。
然后运行我们的测试类,(那些统计代码会进行添加信息到ser文件)。

注意,Test类本身不能继承其他类,否则会报错。

cobertura下载
http://cobertura.sourceforge.net

下载解压



下载完成后添加
COBERTURA_HOME环境变量



以及设置path



例子


我的项目上如结构

第一步要做的是告诉cobertura去修改class文件
进入到项目目录
执行
E:\java\junit>cobertura-instrument --destination instrumented bin\test
会对bin\test下面的所有文件进行修改。修改后的文件放入instrumented目录(--destination参数指定存放修改后的class文件所在目录)
并生成ser文件。

E:\java\junit>java -cp lib\*;%COBERTURA_HOME%\cobertura.jar;instrumented;bin -Dne
t.sourceforge.cobertura.datafile=cobertura.ser org.junit.runner.JUnitCore test.C
alculatorTest

其中-Dne
t.sourceforge.cobertura.datafile=cobertura.ser指定测试信息写入到cobertura.ser档案文件。

生成报表
E:\java\junit>cobertura-report --format html --datafile cobertura.ser --destination reports src

--destination reports src指定报表文件存放在reports目录,源码文件从src中读取。

另外如果是使用utf-8编码的话,可能会出现如下错误

net.sourceforge.cobertura.javancss.parser.TokenMgrError: Lexical error at line 1
2, column 61.  Encountered: "\r" (13), after : "\"\u7f01\u64b4\u7049\u6d93\u5d86
\ue11c\u7ead\ufffd, result,1, 0);"
        at net.sourceforge.cobertura.javancss.parser.JavaParserTokenManager.getN
extToken(JavaParserTokenManager.java:2078)
        at net.sourceforge.cobertura.javancss.parser.JavaParser.jj_scan_token(Ja
vaParser.java:10181)
        at net.sourceforge.cobertura.javancss.parser.JavaParser.jj_3R_198(JavaPa
rser.java:8524)
这需要为文件指定默认编码。也就是加入-Dfile.encoding=utf-8 参数
修改cobertura-report.bat文件,更改

java -cp "%COBERTURA_HOME%cobertura.jar;%COBERTURA_HOME%lib\asm-3.0.jar;%COBERTURA_HOME%lib\asm-tree-3.0.jar;%COBERTURA_HOME%lib\log4j-1.2.9.jar;%COBERTURA_HOME%lib\jakarta-oro-2.0.8.jar"  net.sourceforge.cobertura.reporting.Main %CMD_LINE_ARGS%




java -cp "%COBERTURA_HOME%cobertura.jar;%COBERTURA_HOME%lib\asm-

3.0.jar;%COBERTURA_HOME%lib\asm-tree-3.0.jar;%COBERTURA_HOME%

lib\log4j-1.2.9.jar;%COBERTURA_HOME%lib\jakarta-oro-2.0.8.jar" -

Dfile.encoding=utf-8 net.sourceforge.cobertura.reporting.Main %

CMD_LINE_ARGS%

成功后进入reports目录,打开index.html就能看到相关的测试信息。








ant
<?xml version="1.0" encoding="utf-8" ?>

<project name="superschool" default="all" basedir=".">
<property environment="env" />
<property name="projectName" value="superschool" />
<property name="src.dir" value="src/main/java" />
<property name="resource.dir" value="resource" />
<property name="build.dir" value="build" />
<property name="build.classes" value="${build.dir}/classes" />
<property name="webroot.dir" value="WebContent" />
<property name="lib.dir" value="${webroot.dir}/WEB-INF/lib" />
<property name="lib.test.dir" value="test_lib" />
<property name="compiler.lib.dir" value="compiler_lib" />
<property name="target.dir" value="${build.dir}/target" />
<property name="target.app" value="${build.dir}/target/${projectName}" />
<property name="deploy.dir" value="${user.home}/${projectName}/webapp" />

<path id="class.path">
<pathelement location="${build.classes}" />
<fileset dir="${lib.dir}" includes="**/*.jar" />
<fileset dir="${compiler.lib.dir}" includes="**/*.jar" />
<fileset dir="${lib.test.dir}" includes="**/*.jar">
</fileset>
</path>

<target name="clean" description="清理应用">
<description>
清理应用的build目录
</description>

<echo message="开始清理应用" />

<tstamp>
<format property="starttime_clean" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>

<echo message="开始时间:${starttime_clean}" />

<delete dir="${build.dir}" />
<delete dir="${deploy.dir}" />

<tstamp>
<format property="endtime_clean" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>

<echo message="应用清理完毕" />

<echo message="结束时间:${endtime_clean}" />
<echo message="------------------------------" />
<echo message="" />
</target>

<target name="compiler" description="编译项目">
<description>
编译项目
compiler.lib.dir特性指向的目录是存放编译需要的架包,
这个目录里面的架包是不会放到部署目录的lib目录的。
</description>

<tstamp>
<format property="starttime_compiler" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>

<echo message="开始编译" />
<echo message="开始时间:${starttime_compiler}" />

<mkdir dir="${build.dir}" />
<mkdir dir="${build.classes}" />

<!--编译源文件-->
<javac srcdir="${src.dir}" destdir="${build.classes}" debug="true" encoding="utf-8">
<classpath refid="class.path" />
</javac>

<!--拷贝配置文件-->
<copy todir="${build.classes}">
<fileset dir="${resource.dir}">
<exclude name="**/*.class" />
<exclude name="**/*.java" />
<exclude name="**/jdbc.properties" />
<exclude name="**/log4j.xml" />
</fileset>
</copy>

<property name="begintoken" value="$" />

<!--配置文件properties的antx功能-->
<copy encoding="utf-8" todir="${build.classes}/com/superschool/resource/spring/properties/jdbc">
<fileset dir="config">
<include name="jdbc.properties" />
</fileset>

<!--定义拷贝替换过滤器-->
<filterset begintoken="${begintoken}{" endtoken="}">
<filtersfile file="${user.home}/${projectName}/antx.properties" />
</filterset>
</copy>

<copy encoding="utf-8" todir="${build.classes}/com/superschool/resource/log4j">
<fileset dir="config">
<include name="log4j.xml" />
</fileset>

<filterset begintoken="${begintoken}{" endtoken="}">
<filtersfile file="${user.home}/${projectName}/antx.properties" />
</filterset>
</copy>

<tstamp>
<format property="endtime_compiler" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>

<echo message="编译成功结束" />
<echo message="结束时间:${endtime_compiler}" />
<echo message="------------------------------" />
<echo message="" />
</target>

<target name="make-app" description="生成项目文件夹">
<tstamp>
<format property="starttime_make-app" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>

<echo message="开始生成app" />
<echo message="开始时间:${starttime_make-app}" />

<mkdir dir="${target.app}" />
<mkdir dir="${target.app}/WEB-INF/lib" />
<mkdir dir="${target.app}/WEB-INF/classes" />

<!--拷贝app的WebContent目录到下面的文件到项目目录-->
<copy todir="${target.app}">
<fileset dir="${webroot.dir}" />
</copy>

<!--拷贝app的classes目录到下面的文件到项目目录的classes-->
<copy todir="${target.app}/WEB-INF/classes">
<fileset dir="${build.classes}">
<include name="**/**" />
</fileset>
</copy>

<!--拷贝app的lib目录到下面的文件到项目目录的lib-->
<copy todir="${target.app}/WEB-INF/lib">
<fileset dir="${lib.dir}" includes="**/*.jar" />
</copy>

<tstamp>
<format property="endtime_make-app" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>

<echo message="app生成结束" />
<echo message="结束时间:${endtime_make-app}" />
<echo message="------------------------------" />
<echo message="" />
</target>

<target name="test" if="test">
<property name="cobertura.home" value="${env.COBERTURA_HOME}" />
<property name="src.test.dir" value="src/test/java" />
<property name="test.report.dir" value="${user.home}/${projectName}/test/report" />
<property name="test.report.html" value="${user.home}/${projectName}/test/report/html" />
<property name="cobertura.dir" value="${user.home}/${projectName}/test/cb" />

<path id="class.test.path">
<fileset dir="${lib.test.dir}" includes="**/*.jar">
</fileset>
</path>

<!--代码测试覆盖率配置class路径-->
<path id="class.cobertura.path">
<fileset dir="${cobertura.home}">
<include name="cobertura.jar" />
<include name="lib/**/*.jar" />
</fileset>
</path>

<delete dir="${test.report.dir}" />
<delete dir="${test.report.html}" />
<delete file="${cobertura.dir}" />
<mkdir dir="${test.report.dir}" />
<mkdir dir="${test.report.html}" />
<mkdir dir="${cobertura.dir}" />

<!--代码覆盖率-->
<taskdef classpathref="class.cobertura.path" resource="tasks.properties" />

<tstamp>
<format property="starttime_test_cobertura" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>

<echo message="开始生成测试代码覆盖率" />
<echo message="开始时间:${starttime_test_cobertura}" />

<cobertura-instrument todir="${build.classes}">
<ignore regex="org.apache.log4j.*" />

<fileset dir="${build.classes}">
<include name="**/*.class" />
<exclude name="**/*Test*.class" />
</fileset>
</cobertura-instrument>

<tstamp>
<format property="endtime_test_cobertura" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>

<echo message="测试代码覆盖率生成结束" />
<echo message="结束时间:${endtime_test_cobertura}" />
<echo message="------------------------------" />
<echo message="" />


<!--编译测试类-->
<tstamp>
<format property="starttime_test_compiler" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>

<echo message="开始编译测试代码" />
<echo message="开始时间:${starttime_compiler}" />

<javac srcdir="${src.test.dir}" destdir="${build.classes}" debug="true" encoding="utf-8">
<classpath refid="class.path" />
<classpath refid="class.test.path" />
</javac>

<tstamp>
<format property="endtime_test_compiler" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>

<echo message="编译测试代码结束" />
<echo message="结束时间:${endtime_test_compiler}" />
<echo message="------------------------------" />
<echo message="" />

<!--运行测试-->
<tstamp>
<format property="starttime_test_run" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>
<echo message="开始运行测试代码" />
<echo message="开始时间:${starttime_test_run}" />
<!--此处必须让fork=yes,在一个新的vm中运行,否则,需要运行该build文件两次才能得到cobertura的html报表-->
<junit fork=yes printsummary="yes" haltonerror="yes" haltonfailure="yes" fork="no">
<formatter type="plain" usefile="false" />
<formatter type="xml" usefile="true" />

<batchtest todir="${test.report.dir}">
<fileset dir="${src.test.dir}">
<include name="**/Test*.java" />
</fileset>
</batchtest>

<classpath refid="class.path" />
<classpath refid="class.test.path" />
<classpath refid="class.cobertura.path">
</classpath>
</junit>

<!--<junit printsummary="yes" haltonerror="yes" haltonfailure="yes" fork="yes">
<formatter type="xml" />
<test name="s.A" todir="${test.report.dir}" />
<classpath refid="class.path" />
</junit>-->

<tstamp>
<format property="endtime_test_run" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>

<echo message="运行测试代码结束" />
<echo message="结束时间:${endtime_test_run}" />
<echo message="------------------------------" />
<echo message="" />

<!--生成报表-->
<tstamp>
<format property="starttime_test_html" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>
<echo message="开始生成测试报表" />
<echo message="开始时间:${starttime_test_html}" />
<mkdir dir="${test.report.html}" />

<junitreport todir="${test.report.dir}">
<fileset dir="${test.report.dir}">
<include name="TEST-*.xml" />
</fileset>
<report todir="${test.report.dir}/html" />
</junitreport>

<tstamp>
<format property="endtime_test_html" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>

<echo message="生成测试报表结束" />
<echo message="结束时间:${endtime_test_html}" />
<echo message="------------------------------" />
<echo message="" />

<tstamp>
<format property="starttime_cobertura_report_html" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>
<echo message="开始生成测试覆盖率报表" />
<echo message="开始时间:${starttime_cobertura_report_html}" />
<mkdir dir="${test.report.html}" />

<cobertura-report srcdir="${src.dir}" destdir="${cobertura.dir}" format="html" />

<!--删除报表主文件-->
<delete file="cobertura.ser">
</delete>
<tstamp>
<format property="endtime_cobertura_report_html" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>

<echo message="生成测试覆盖率报表结束" />
<echo message="结束时间:${endtime_cobertura_report_html}" />
<echo message="------------------------------" />
<echo message="" />


</target>

<target name="deploy" description="部署应用">
<tstamp>
<format property="starttime_deploy" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>

<echo message="开始部署" />
<echo message="开始时间:${starttime_deploy}" />

<delete dir="${deploy.dir}" />
<mkdir dir="${deploy.dir}" />

<copy todir="${deploy.dir}">
<fileset dir="${target.app}" />
</copy>

<echo message="部署结束" />

<tstamp>
<format property="endtime_deploy" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>

<echo message="结束时间:${endtime_deploy}" />
<echo message="------------------------------" />
<echo message="" />
</target>

<target name="all" description="所有操作" depends="clean, compiler, make-app, deploy, test">
<echo message="所有任务完成" />
</target>
</project>
  • 大小: 73.6 KB
  • 大小: 14 KB
  • 大小: 3.9 KB
  • 大小: 46.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics