Java Coverage(Cobertura)工具
首先是下载Cobertura的jar包了,这个工具底层是JCoverage,熟悉Jcoverage的对这个也不会陌生的。
Cobertura官网 http://cobertura.sourceforge.net/
大家可以了解很多东西,比如现在的作者啊什么,这里就不介绍了
然后点Download,下载二进制版本,比如名字叫cobertura-1.9.4.1(我用的是最新的version)
最后就是实践了。。下面就给大家讲解一下这个到底是什么玩意
Cobertura是一个开源的java工具,它主要用在java test中 内部封装了JCoverage
它是一个用ant全自动的测试工具,很强大。
如果你想测试覆盖率的话,用这个软件是相当不错的
那么下面就举一个简单的例子来说明它的强大
以下为要测试的Java类
- package com.example.simple;
- /**
- * @author cnchenhl 2011/02/25
- */
- public class PrintStringOfYourName {
- private String name = null;
- private int id = 0;
- private String sex = null;
- private int age = 0;
- private String address;
- public String getName() {
- return name;
- }
- public int getId() {
- return id;
- }
- public String getSex() {
- return sex;
- }
- public int getAge() {
- return age;
- }
- public String getAddress() {
- return address;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setId(int id) {
- this.id = id;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public String toString() {
- return "Name :" + name + "ID :" + id + "Age :" + age + "Sex :" + sex + "Address :" + address;
- }
- }
下面是测试的类test
- package com.example.simple;
- import junit.framework.TestCase;
- import org.junit.Before;
- import org.junit.Test;
- /**
- *
- * @author cnchenhl 2011/02/25
- */
- public class PrintStringOfYourNameTest extends TestCase {
- @Before
- public void setUp() throws Exception {
- System.setProperty("Dnet.sourceforge.cobertura.datafile", "D:/TestCorbertura/cobertura.ser");
- }
- @Test
- public void testtoString() {
- PrintStringOfYourName printStringOfYourName = new PrintStringOfYourName();
- printStringOfYourName.setAddress("shanghai");
- printStringOfYourName.setAge(24);
- printStringOfYourName.setId(0);
- printStringOfYourName.setName("chenhailong");
- printStringOfYourName.setSex("male");
- String str = printStringOfYourName.toString();
- assertEquals(str, "Name :chenhailongID :0Age :24Sex :maleAddress :shanghai");
- }
- }
好了准备工作完成了
那就准备自己的ant构建文件了。路径大家要仔细看,可能会有问题的,基本都是你们的路径有问题。
还要说明的一点是
- System.setProperty("Dnet.sourceforge.cobertura.datafile", ""D:/TestCorbertura/cobertura.ser"");
是必须加的,官网指定的,大家要注意。因为instruments容易找不到cobertura.ser 文件,在ant中也要设定
具体的请看构建文件build.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <project name="cobertura.examples.basic" default="coverage" basedir=".">
- <property file="build.properties" />
- <property name="bin.dir" value="${basedir}/bin" />
- <property name="target" value="target" />
- <path id="cobertura.classpath">
- <fileset dir="${cobertura.dir}">
- <include name="cobertura.jar" />
- <include name="lib/**/*.jar" />
- </fileset>
- </path>
- <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>
- <target name="init">
- <mkdir dir="${classes.dir}" />
- <mkdir dir="${instrumented.dir}" />
- <mkdir dir="${reports.xml.dir}" />
- <mkdir dir="${reports.html.dir}" />
- <mkdir dir="${coverage.xml.dir}" />
- <mkdir dir="${coverage.summaryxml.dir}" />
- <mkdir dir="${coverage.html.dir}" />
- <mkdir dir="${target}" />
- </target>
- <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">
- <echo message="copy the file to the classes" />
- <copy todir="${classes.dir}">
- <fileset dir="${bin.dir}">
- <include name="**/*.class" />
- </fileset>
- </copy>
- <delete file="cobertura.ser" />
- <delete dir="${instrumented.dir}" />
- <cobertura-instrument todir="${instrumented.dir}">
- <ignore regex="org.apache.log4j.*" />
- <fileset dir="${classes.dir}">
- <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">
- <classpath location="${instrumented.dir}" />
- <classpath location="${classes.dir}" />
- <classpath >
- </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">
- <cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />
- </target>
- <target name="summary-coverage-report">
- <cobertura-report srcdir="${src.dir}" destdir="${coverage.summaryxml.dir}" format="summaryXml" />
- </target>
- <target name="alternate-coverage-report">
- <cobertura-report destdir="${coverage.html.dir}">
- <fileset dir="${src.dir}">
- <include name="**/*.java"/>
- </fileset>
- </cobertura-report>
- </target>
- <target name="clean" description="Remove all files created by the build/test process.">
- <delete dir="${classes.dir}" />
- <delete dir="${instrumented.dir}" />
- <delete dir="${reports.dir}" />
- <delete file="cobertura.log" />
- <delete file="cobertura.ser" />
- <delete dir="${target}" />
- </target>
- <target name="coverage" depends="compile,instrument,test,coverage-report,summary-coverage-report,alternate-coverage-report,band" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports." />
- <target name="band">
- <delete file="${target}/report.zip" />
- <jar destfile="${target}/report.zip" basedir="${reports.dir}">
- <manifest>
- <attribute name="Build-Time" value="${timeStamp.day}" />
- </manifest>
- </jar>
- </target>
- </project>
下面是构建文件需要的环境变量
- # The source code for the examples can be found in this directory
- srcsrc.dir=src
- # The path to cobertura.jar
- cobertura.dir=D:/cobertura-1.9.4.1
- # Classes generated by the javac compiler are deposited in this directory
- classesclasses.dir=classes
- # Instrumented classes are deposited into this directory
- instrumentedinstrumented.dir=instrumented
- # All reports go into this directory
- reportsreports.dir=reports
- # Unit test reports from JUnit are deposited into this directory
- reports.xml.dir=${reports.dir}/junit-xml
- reports.html.dir=${reports.dir}/junit-html
- # Coverage reports are deposited into these directories
- coverage.xml.dir=${reports.dir}/cobertura-xml
- coverage.summaryxml.dir=${reports.dir}/cobertura-summary-xml
- coverage.html.dir=${reports.dir}/cobertura-html
转载于:https://blog.51cto.com/justlpf/1191253
Java Coverage(Cobertura)工具相关推荐
- java coverage_Java Coverage(Cobertura)工具
首先是下载Cobertura的jar包了,这个工具底层是JCoverage,熟悉Jcoverage的对这个也不会陌生的. 大家可以了解很多东西,比如现在的作者啊什么,这里就不介绍了 然后点Downlo ...
- Java应用DevOps工具链推荐
文章目录 Java应用DevOps工具链推荐 前言 Java应用DevOps工具链 扩展阅读 Java应用DevOps工具链推荐 前言 本文列出了Java应用常用的DevOps工具链,供大家参考. 涉 ...
- Java代码自动化测试工具Parasoft Jtest 2021全新发布,支持更多IDE
Parasoft Jtest通过提供一套工具来加速Java软件开发,以保证您的软件可靠,安全和可维护,从而最大限度地提高质量并最大限度地降低业务风险.全面且可配置的报告使开发人员和管理人员能够理解并优 ...
- java安卓开发工具_推荐几个非常实用的Android开发工具
工欲善其事,必先利其器.我们进行Android开发也要有好的开发工具辅助才能更好更高效的完成各种开发,为用户提供更实用的应用程序.本文就为大家推荐几个非常实用的Android开发工具,及几个常用的编辑 ...
- 分享五款java学习辅助工具,总有你用的上的~
想要学好java技术,除了自身的努力,辅助工具也不缺少,辅助工具可以帮助大家在今后的工作中可以提高工作效率,下面小编就来分享五款java学习辅助工具,总有你用的上的~ 五款java学习辅助工具: 1. ...
- Java XML解析工具 dom4j介绍及使用实例
Java XML解析工具 dom4j介绍及使用实例 dom4j介绍 dom4j的项目地址:http://sourceforge.net/projects/dom4j/?source=directory ...
- Java封装OkHttp3工具类
点击关注公众号,Java干货及时送达 作者:如漩涡 https://blog.csdn.net/m0_37701381 Java封装OkHttp3工具类,适用于Java后端开发者 说实在话,用过挺多 ...
- 一些Java反编译工具/源代码查看工具的介绍
2019独角兽企业重金招聘Python工程师标准>>> 有的朋友抱怨他们在使用他们公司的闭源框架时看不到底层的源代码.那么可以尝试使用一些Java反编译工具. 下面我举个例子介绍具体 ...
- 细说Java主流日志工具库
细说 Java 主流日志工具库 日志框架 java.util.logging (JUL) Log4j Logback Log4j2 Log4j vs Logback vs Log4j2 日志门面 co ...
最新文章
- Linq 集合处理(Union)
- UA MATH567 高维统计II 随机向量2 各向同性的随机向量
- Windows Server Core管理之WinRM
- sentinel的@SentinelResource注解使用
- AtCoder Beginner Contest 192 F - Potion 背包dp
- 又一任务被Transformer攻陷!NVIDIA开源HORST,用Transformer解决早期动作识别和动作预期任务...
- Windows 2003超级终端
- Abobe Flash cs6 和 Abobe Flash Builder4.6 破解和许可证过期
- python 完全背包问题_完全背包问题及Python代码实现
- 获取U盘 VID,PID
- 现汇买入价/现钞买入价/卖出价/基准价/中行折算价的概念
- 西门子——不同数据的存储方式
- 三星android pie更新,三星Android Pie更新路线图公布 Galaxy Note9需等明年二月
- apmserv5.2.6 mysql启动失败_win7(xp) APMServ5.2.6 Apache启动失败,MYSQL启动失败 的解决办法...
- 微信小程序 图片旋转后上传
- redis incr命令最大值问题
- Fabric背书过程中链码是并行还是串行?
- X-Powered-By: Servlet/3.0漏洞修复
- 红帽第四季度订阅的强劲增长 整体表现超预期
- Oracle在中国的裁员暗示了中国的云服务市场什么?
热门文章
- python调用cmd命令会弹出黑框_python 调用cmd,不显示cmd黑框
- html css js实现快递单打印_html+css+js实现计算器
- 情人节 html5,情人节H5案例 | 2019第一波情人节营销已上线
- mimo的误码率_揭晓MU-MIMO黑科技!
- 电脑机时,电脑死机时,为啥会忍不住扇它一巴掌?
- ios share extension 真机不显示_ios企业签名:APPGroups实现App之间数据共享
- python特性和属性_Python之属性、特性和修饰符
- 使用SpringBoot发送邮件 在本地测试是好的 放到服务器连接超时问题
- Spring之注解方式实例化Java类
- ios技术篇-CoreData