- 浏览: 365002 次
- 性别:
- 来自: 西安
文章分类
最新评论
-
jiangli19192:
...
自己写的一个启动JBoss服务器的bat批处理 -
56553655:
最好这样:java -Xms3700M -Xmx3700M - ...
测试本机JVM支持的最大内存 -
lizhiy05:
学习一哈……
Web Services体系结构及相关概念 -
ghy200692162:
System.out.println("开始注册Js ...
基于OSGi的JSF Web组件开发问题求解 -
xiao888lin:
你的头像看起来很像我们宿舍老四。。。
测试本机JVM支持的最大内存
这次还是配置问题,接上上次关于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>
Cobertura 是敏捷程序员工具箱中新增的一个重要工具。通过生成代码覆盖率的具体数值,Cobertura 将单元测试从一种艺术转变为一门科学。它可以寻找测试覆盖中的空隙,直接找到 bug。测量代码覆盖率使您可以获得寻找并修复 bug 所需的信息,从而开发出对每个人来说都更健壮的软件。
尽管测试先行编程(test-first programming)和单元测试已不能算是新概念,但测试驱动的开发仍然是过去 10 年中最重要的编程创新。最好的一些编程人员在过去半个世纪中一直在使用这些技术,不过,只是在最近几年,这些技术才被广泛地视为在时间及成本预算内开发健壮的无缺陷软件的关键所在。但是,测试驱动的开发不能超过测试所能达到的程度。测试改进了代码质量,但这也只是针对实际测试到的那部分代码而言的。您需要有一个工具告诉您程序的哪些部分没有测试到,这样就可以针对这些部分编写测试代码并找出更多 bug。 Mark Doliner 的 Cobertura (cobertura 在西班牙语是覆盖的意思)是完成这项任务的一个免费 GPL 工具。Cobertura 通过用额外的语句记录在执行测试包时,哪些行被测试到、哪些行没有被测试到,通过这种方式来度量字节码,以便对测试进行监视。然后它生成一个 HTML 或者 XML 格式的报告,指出代码中的哪些包、哪些类、哪些方法和哪些行没有测试到。可以针对这些特定的区域编写更多的测试代码,以发现所有隐藏的 bug。
发表评论
-
GIT使用批处理完成日常代码管理
2017-03-26 22:01 1228本文默认读者的本机是已经安装好了GIT本地端,因此对于GI ... -
在Maven2插件中用Velocity对配置文件的集中管理
2007-08-06 16:55 2352正如我在“用Velocity进行配置文件信息的集中管理 ”—— ... -
用Velocity进行配置文件信息的集中管理
2007-07-27 16:02 4565Apache从他诞生的那天起 ... -
测试驱动开发与EasyMock的使用
2007-07-17 22:19 1882测试驱动开发并不是什 ... -
在工程POM中内嵌Ant脚本
2007-07-17 22:14 1729由于每篇文章的字数有限制,我没办法在同一个专题中同时将相关的问 ... -
Maven2插件开发详解
2007-07-17 22:11 8906在Maven2强大功能的魅力 ... -
用Emma的Eclipse插件进行代码覆盖率测试
2007-07-17 22:00 6858如上一个关于Cobertura的专题所说,我这里单开一个专题来 ... -
用Cobertura在Maven命令行进行覆盖测试
2007-07-17 21:58 8993Cobertura是一个基于jcovera ... -
我写的ant编译、打包、测试、测试报告生成和邮件发送模板
2007-07-17 21:40 4915js 代码 <?xml ve ... -
XFire使用举例
2007-07-17 21:32 2376闲来无事,顺便写个XFire的小例子,也算是打发时间了,o( ... -
Maven入门--较复杂的实例
2007-07-17 21:19 1602本文将使用一个较复杂的实例,讲述如何定制目录布局(即不使用M ... -
Maven入门--概念与实例
2007-07-17 21:15 1298Maven入门--概念与实例 最近由于工作原因在研 ... -
设计模式总结
2007-07-17 17:14 628设计模式 1) 控制倒置模式: 说到控制倒置模式,就不得不 ... -
Eclipse中常用快捷键总结
2007-07-17 16:57 1142Eclipse中常用快捷键总结: CTRL+SHI ... -
修改eclipse中的默认maven2资源仓库保存地址
2007-07-17 16:56 5809当你在eclipse中集成了maven2后,默认情况下,ecl ... -
用ant进行工程管理
2007-07-17 13:50 21021.典型地,一个ant工程脚本如下: < ... -
利用maven2为工程的jar文件内部打入版本信息
2007-07-17 13:42 17221.修改pom.xml文件,在proj ... -
比ant更强大的工具maven2之自动生成工程
2007-07-17 13:38 2921maven2是在ant的基础上发展起来的,并对ant的功能进行 ... -
OFBIZ开源电子商务学习心得
2007-07-17 13:36 2521看懂在APACHE网站上的英文文档确实是一项不小的挑战,下面说 ... -
开始每个模块功能编写时需要做的事情总结
2007-07-17 13:20 10881.对查询出的展示列表进行排序 2.检查SQL语句,并思 ...
相关推荐
Cobertura代码覆盖率测试工具介绍文档,Cobertura代码覆盖率测试工具介绍
cobertura和weblogic整合使用-代码覆盖率测试 里面详细步骤和使用说明,并附加xml文件,不懂的可以QQ我,147806015,
Junit+ant+cobertura示例源码程序。
NULL 博文链接:https://terrencexu.iteye.com/blog/718834
cobertura代码覆盖率工具,有工具包,并且付上一份自己给项目组写的手册一份。
Cobertura是一个测试案例覆盖率统计工具,此资源下载后,解压,配置上环境变量即可使用
代码测试覆盖率工具,该工具与ant执行集成,快速简单有效的测试代码的覆盖率,内附测试用例
Cobertura 是一种代码覆盖测试工具,它通过检测基本的代码,并观察在测试包运行时执行了哪些代码和没有执行哪些代码,来测量测试覆盖率。
NULL 博文链接:https://mwhgjava.iteye.com/blog/1297276
它也旨在通过pycobertura diff命令来防止代码覆盖率降低:任何更改的行都应进行测试,并且未覆盖的更改应清晰可见,而不会让遗留的未覆盖代码成为pycobertura diff因此开发人员可以仅专注于其更改。 特征: 显示...
示例build文件 博文链接:https://zhupan.iteye.com/blog/200878
Cobertura是一种开源工具,它通过检测基本的代码,并观察在测试包运行时执行了哪些代码和没有执行哪些代码,来测量测试覆盖率。除了找出未测试到的代码并发现bug外,Cobertura还可以通过标记无用的、执行不到的代码...
cobertura测试代码
JSCover 是一个易于使用的 JavaScript 代码覆盖率测量工具。它是流行的 JSCoverage工具的增强版本,增加了一些功能,包括分支覆盖率、 LCOV和用于 CI 集成的Cobertura XML报告、用于自动化测试的钩子和HTML 本地存储...
Cobertura 也提供一个 Ant 任务和 Maven 插件,但用 Cobertura,您可以在编写代码时 评估代码覆盖率。您见过这样的模式吗? 通过选择 Eclipse 菜单项 Run 安装 Coverlipse 插件并将其和 JUnit 关联起来,该操作会...
使用下面的代码片段运行测试套件,以便在终端输出中显示单元测试结果和代码覆盖率统计信息。 还将生成一些 Jenkins 友好的 XML 结果文件(用于单元测试的 JUnit 和用于代码覆盖率的 Cobertura)。 node_modules/...
Cobertura可用于分析Java代码的测试覆盖率。Cobertura根据没有被测试覆盖的代码生成基于HTML的报告。 Cobertura提供可用于插装、检查和测试代码的工具。通过监控可测试的代码,Cobertura允许你使用你选择的测试框架...
三者结合,利用EMMA产生的测试覆盖度报考,可以一目了然的知道那部分代码已经被测试,哪部分没有,从而控制单元测试的质量。 EMMA,它是一个免费、开源的、用来衡量单元测试覆盖度的工具。 三者结合,利用EMMA...