您的位置:首页 > 编程语言

ant中使用cobertura分析测试用例的代码覆盖率

2015-05-05 16:17 519 查看
这次还是配置问题,接上上次关于ant脚本模板的详细说明。对于一个完整的项目测试报告,一般来说我们会用JUnit生成的report来分析关于测试用例执行情况,但是,我们怎么样保证我们的测试用例的测试质量呢,我们如何知道我们的测试用例到底覆盖了多少我们的工程代码呢。Cobertura就是一个很好的开源免费插件,他不仅仅支持ant,而且对maven的支持也有很不错的表现。对于Cobertura对Maven的支持我会在下一个专题中专门阐述(官方提供的Maven plug-in有些小bug,别走开,下一专题告诉你,呵呵呵呵),我这篇文章只针对Cobertura在ant中的使用做一个说明。

在上一个关于ant脚本的专题中,我详细说明了如何用ant来完成mail→mkzip→report→junit→build→prepare→clean等一系列的工作,为了保证项目中能够使用Cobertura,我这次对上一个专题的ant模板进行适当的修改:mail→mkzip→coverage-report→cover-test→instrument→report→junit→build→prepare→clean

我增加了coverage-report(生成测试报告)、cover-test(进行覆盖率测试)和instrument(生成打过标签的二进制classes)等三个target。同时对mkzip和mail的target也进行了适当的修改,以便能够把进行覆盖测试的report进行打包发邮件。

首先定义一个顶级 taskdef 元素将 cobertura.jar 文件限定在当前工作目录中:

<taskdef classpath="cobertura.jar" resource="tasks.properties" />

接下来需要定义一个instrument任务,该任务将在已经编译好的类文件中添加日志代码-打上标签。todir 属性指定将测量类放到什么地方。fileset 子元素指定测量哪些 .class 文件

<target name="instrument" depends="report">

<cobertura-instrument todir="${instrumented.dir}">

<fileset dir="${pro.build.path}">

<include name="**/*.class" />

<exclude name="**/*Test.class" />

</fileset>

</cobertura-instrument>

</target>

接下来就可以进行覆盖测试了,定义一个cover-test任务,该任务依赖于instrument任务。要注意的一点就是被测量的类必须在原始类出现在类路径中之前出现在类路径中,而且需要将 Cobertura JAR 文件添加到类路径中。

<target name="cover-test" depends="instrument">

<mkdir dir="${testreportdir}" />

<junit dir="./" failureproperty="test.failure" printSummary="yes" fork="true" haltonerror="true">

<classpath location="cobertura.jar" />

<classpath location="instrumented" />

<classpath>

<fileset dir="lib">

<include name="**/*.jar" />

</fileset>

<pathelement path="${pro.build.path}" />

<pathelement path="${pro.build.path}" />

</classpath>

<batchtest todir="${pro.build.path}">

<fileset dir="src/main/test">

<include name="**/*Test.java" />

</fileset>

</batchtest>

</junit>

</target>

生成测试报告,为了能够使ant支持cobertura-report,同上个专题说到的一样,我们需要将Cobertura的相关资源文件在${ANT_HOME}/lib下放一份(cobertura-report不是ant内置标签):

<target name="coverage-report" depends="cover-test">

<cobertura-report srcdir="src/main/java" destdir="instrumented" />

</target>

修改打包target

<target name="mkzip" depends="coverage-report">

<jar destfile="report/html/test-result${ant.project.name}.zip">

<fileset dir="report/html">

<include name="**/*.html" />

<include name="**/*.css" />

<include name="**/*.txt" />

</fileset>

</jar>

<jar destfile="report/html/cover-test-result${ant.project.name}.zip">

<fileset dir="instrumented">

<include name="**/*.html" />

<include name="**/*.css" />

<include name="**/*.txt" />

<include name="**/*.png" />

<include name="**/*.js" />

</fileset>

</jar>

</target>

修改发邮件target

<target name="mail" depends="mkzip">

<mail mailhost="smtp.126.com" mailport="25" subject="The Build Test" user="用户名" password="邮箱密码">

<from address="你的发信地址" name="Danlley Wei" />

<fileset dir="report/html">

<include name="**/test-result${ant.project.name}.zip" />

<include name="**/cover-test-result${ant.project.name}.zip" />

</fileset>

<to address="收信人地址" name="Danlley Wei" />

<message>The ${pro.name}--${pro.author} has been tested ! </message>

</mail>

</target>

最后执行一下看看结果。

本专题完整模板

<?xml version="1.0"?>

<project name="springproj" basedir="." default="mail">

<!--<property file="build.properties" /> -->

<property name="pro.name" value="springproj" />

<property name="pro.author" value="Danlley Wei" />

<property name="src.dir" value="src/main/java" />

<property name="pro.web.root" value="war" />

<property name="pro.web.source" value="${pro.web.root}/WEB-INF" />

<property name="pro.build.path" value="${pro.web.source}/classes" />

<property name="user.dir" value="${pro.build.path}" />

<property name="instrumented.dir" value="instrumented" />

<taskdef classpathref="master-classpath" resource="tasks.properties" />

<taskdef classpath="cobertura.jar" resource="tasks.properties" />

<target name="mail" depends="mkzip">

<mail mailhost="smtp.126.com" mailport="25" subject="The Build Test" user="邮箱用户名" password="邮箱密码">

<from address="发信地址" name="Danlley Wei" />

<fileset dir="report/html">

<include name="**/test-result${ant.project.name}.zip" />

<include name="**/cover-test-result${ant.project.name}.zip" />

</fileset>

<to address="收信地址" name="Danlley Wei" />

<message>The ${pro.name}--${pro.author} has been tested ! </message>

</mail>

</target>

<target name="mkzip" depends="coverage-report">

<jar destfile="report/html/test-result${ant.project.name}.zip">

<fileset dir="report/html">

<include name="**/*.html" />

<include name="**/*.css" />

<include name="**/*.txt" />

</fileset>

</jar>

<jar destfile="report/html/cover-test-result${ant.project.name}.zip">

<fileset dir="instrumented">

<include name="**/*.html" />

<include name="**/*.css" />

<include name="**/*.txt" />

<include name="**/*.png" />

<include name="**/*.js" />

</fileset>

</jar>

</target>

<target name="coverage-report" depends="cover-test">

<cobertura-report srcdir="src/main/java" destdir="instrumented" />

</target>

<target name="cover-test" depends="instrument">

<mkdir dir="${testreportdir}" />

<junit dir="./" failureproperty="test.failure" printSummary="yes" fork="true" haltonerror="true">

<classpath location="cobertura.jar" />

<classpath location="instrumented" />

<classpath>

<fileset dir="lib">

<include name="**/*.jar" />

</fileset>

<pathelement path="${pro.build.path}" />

<pathelement path="${pro.build.path}" />

</classpath>

<batchtest todir="${pro.build.path}">

<fileset dir="src/main/test">

<include name="**/*Test.java" />

</fileset>

</batchtest>

</junit>

</target>

<target name="instrument" depends="report">

<cobertura-instrument todir="${instrumented.dir}">

<fileset dir="${pro.build.path}">

<include name="**/*.class" />

<exclude name="**/*Test.class" />

</fileset>

</cobertura-instrument>

</target>

<target name="report" depends="junit">

<junitreport id="myJUnitReport" taskname="reported" todir="report" description="Junit Report">

<fileset dir="report">

<include name="TEST-*.xml" />

</fileset>

<report todir="report/html" />

</junitreport>

</target>

<target name="junit" depends="build">

<mkdir dir="report/html" />

<junit printsummary="yes" haltonerror="yes" haltonfailure="yes" fork="yes">

<classpath location="${build.instrumented.dir}" />

<formatter type="plain" usefile="false" />

<formatter type="xml" />

<test name="org.danlley.hibernate.dao.DeptDAOImplTest" todir="report" />

<classpath refid="master-classpath" />

</junit>

</target>

<target name="build" depends="prepare">

<javac destdir="${pro.build.path}" target="1.5" debug="true">

<src path="src/main/java" />

<classpath refid="master-classpath" />

</javac>

<javac destdir="${pro.build.path}" target="1.5">

<src path="src/main/test" />

<classpath refid="master-classpath" />

</javac>

</target>

<target name="prepare" depends="clean">

<copy todir="${pro.build.path}">

<fileset dir="${src.dir}">

<include name="**/*.properties" />

<include name="**/*.xml" />

</fileset>

</copy>

</target>

<target name="clean">

<delete>

<fileset dir="${pro.build.path}">

<include name="**/*.*" />

</fileset>

<fileset dir="report">

<include name="**/*.*" />

</fileset>

<fileset dir="instrumented">

<include name="**/*.*" />

</fileset>

</delete>

</target>

<path id="master-classpath">

<fileset dir="lib">

<include name="*.jar" />

</fileset>

<pathelement path="${pro.build.path}" />

</path>

</project>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: