首先是下载Cobertura的jar包了,这个工具底层是JCoverage,熟悉Jcoverage的对这个也不会陌生的。

Cobertura官网 http://cobertura.sourceforge.net/

大家可以了解很多东西,比如现在的作者啊什么,这里就不介绍了

然后点Download,下载二进制版本,比如名字叫cobertura-1.9.4.1(我用的是最新的version)

最后就是实践了。。下面就给大家讲解一下这个到底是什么玩意

Cobertura是一个开源的java工具,它主要用在java test中 内部封装了JCoverage

它是一个用ant全自动的测试工具,很强大。

如果你想测试覆盖率的话,用这个软件是相当不错的

那么下面就举一个简单的例子来说明它的强大

以下为要测试的Java类

Printstringofyourname代码  
  1. package com.example.simple;
  2. /**
  3. * @author cnchenhl 2011/02/25
  4. */
  5. public class PrintStringOfYourName {
  6. private String name = null;
  7. private int id = 0;
  8. private String sex = null;
  9. private int age = 0;
  10. private String address;
  11. public String getName() {
  12. return name;
  13. }
  14. public int getId() {
  15. return id;
  16. }
  17. public String getSex() {
  18. return sex;
  19. }
  20. public int getAge() {
  21. return age;
  22. }
  23. public String getAddress() {
  24. return address;
  25. }
  26. public void setName(String name) {
  27. this.name = name;
  28. }
  29. public void setId(int id) {
  30. this.id = id;
  31. }
  32. public void setSex(String sex) {
  33. this.sex = sex;
  34. }
  35. public void setAge(int age) {
  36. this.age = age;
  37. }
  38. public void setAddress(String address) {
  39. this.address = address;
  40. }
  41. public String toString() {
  42. return "Name :" + name + "ID :" + id + "Age :" + age + "Sex :" + sex + "Address :" + address;
  43. }
  44. }

下面是测试的类test

Printstringofyournametest代码  
  1. package com.example.simple;
  2. import junit.framework.TestCase;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. /**
  6. *
  7. * @author cnchenhl 2011/02/25
  8. */
  9. public class PrintStringOfYourNameTest extends TestCase {
  10. @Before
  11. public void setUp() throws Exception {
  12. System.setProperty("Dnet.sourceforge.cobertura.datafile", "D:/TestCorbertura/cobertura.ser");
  13. }
  14. @Test
  15. public void testtoString() {
  16. PrintStringOfYourName printStringOfYourName = new PrintStringOfYourName();
  17. printStringOfYourName.setAddress("shanghai");
  18. printStringOfYourName.setAge(24);
  19. printStringOfYourName.setId(0);
  20. printStringOfYourName.setName("chenhailong");
  21. printStringOfYourName.setSex("male");
  22. String str = printStringOfYourName.toString();
  23. assertEquals(str, "Name :chenhailongID :0Age :24Sex :maleAddress :shanghai");
  24. }
  25. }

好了准备工作完成了

那就准备自己的ant构建文件了。路径大家要仔细看,可能会有问题的,基本都是你们的路径有问题。

还要说明的一点是

  1. System.setProperty("Dnet.sourceforge.cobertura.datafile", ""D:/TestCorbertura/cobertura.ser"");

是必须加的,官网指定的,大家要注意。因为instruments容易找不到cobertura.ser 文件,在ant中也要设定

具体的请看构建文件build.xml

Build.xml代码  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project name="cobertura.examples.basic" default="coverage" basedir=".">
  3. <property file="build.properties" />
  4. <property name="bin.dir" value="${basedir}/bin" />
  5. <property name="target" value="target" />
  6. <path id="cobertura.classpath">
  7. <fileset dir="${cobertura.dir}">
  8. <include name="cobertura.jar" />
  9. <include name="lib/**/*.jar" />
  10. </fileset>
  11. </path>
  12. <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>
  13. <target name="init">
  14. <mkdir dir="${classes.dir}" />
  15. <mkdir dir="${instrumented.dir}" />
  16. <mkdir dir="${reports.xml.dir}" />
  17. <mkdir dir="${reports.html.dir}" />
  18. <mkdir dir="${coverage.xml.dir}" />
  19. <mkdir dir="${coverage.summaryxml.dir}" />
  20. <mkdir dir="${coverage.html.dir}" />
  21. <mkdir dir="${target}" />
  22. </target>
  23. <target name="compile" depends="init">
  24. <javac srcdir="${src.dir}" destdir="${classes.dir}" debug="yes">
  25. <classpath refid="cobertura.classpath" />
  26. </javac>
  27. </target>
  28. <target name="instrument" depends="init,compile">
  29. <echo message="copy the file to the classes" />
  30. <copy todir="${classes.dir}">
  31. <fileset dir="${bin.dir}">
  32. <include name="**/*.class" />
  33. </fileset>
  34. </copy>
  35. <delete file="cobertura.ser" />
  36. <delete dir="${instrumented.dir}" />
  37. <cobertura-instrument todir="${instrumented.dir}">
  38. <ignore regex="org.apache.log4j.*" />
  39. <fileset dir="${classes.dir}">
  40. <include name="**/*.class" />
  41. <exclude name="**/*Test.class" />
  42. </fileset>
  43. </cobertura-instrument>
  44. </target>
  45. <target name="test" depends="init,compile">
  46. <junit fork="yes" dir="${basedir}" failureProperty="test.failed">
  47. <classpath location="${instrumented.dir}" />
  48. <classpath location="${classes.dir}" />
  49. <classpath >
  50. </classpath>
  51. <classpath refid="cobertura.classpath" />
  52. <formatter type="xml" />
  53. <test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
  54. <batchtest todir="${reports.xml.dir}" unless="testcase">
  55. <fileset dir="${src.dir}">
  56. <include name="**/*Test.java" />
  57. </fileset>
  58. </batchtest>
  59. </junit>
  60. <junitreport todir="${reports.xml.dir}">
  61. <fileset dir="${reports.xml.dir}">
  62. <include name="TEST-*.xml" />
  63. </fileset>
  64. <report format="frames" todir="${reports.html.dir}" />
  65. </junitreport>
  66. </target>
  67. <target name="coverage-check">
  68. <cobertura-check branchrate="34" totallinerate="100" />
  69. </target>
  70. <target name="coverage-report">
  71. <cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />
  72. </target>
  73. <target name="summary-coverage-report">
  74. <cobertura-report srcdir="${src.dir}" destdir="${coverage.summaryxml.dir}" format="summaryXml" />
  75. </target>
  76. <target name="alternate-coverage-report">
  77. <cobertura-report destdir="${coverage.html.dir}">
  78. <fileset dir="${src.dir}">
  79. <include name="**/*.java"/>
  80. </fileset>
  81. </cobertura-report>
  82. </target>
  83. <target name="clean" description="Remove all files created by the build/test process.">
  84. <delete dir="${classes.dir}" />
  85. <delete dir="${instrumented.dir}" />
  86. <delete dir="${reports.dir}" />
  87. <delete file="cobertura.log" />
  88. <delete file="cobertura.ser" />
  89. <delete dir="${target}" />
  90. </target>
  91. <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." />
  92. <target name="band">
  93. <delete file="${target}/report.zip" />
  94. <jar destfile="${target}/report.zip" basedir="${reports.dir}">
  95. <manifest>
  96. <attribute name="Build-Time" value="${timeStamp.day}" />
  97. </manifest>
  98. </jar>
  99. </target>
  100. </project>

下面是构建文件需要的环境变量

Build.properties代码  
  1. # The source code for the examples can be found in this directory
  2. srcsrc.dir=src
  3. # The path to cobertura.jar
  4. cobertura.dir=D:/cobertura-1.9.4.1
  5. # Classes generated by the javac compiler are deposited in this directory
  6. classesclasses.dir=classes
  7. # Instrumented classes are deposited into this directory
  8. instrumentedinstrumented.dir=instrumented
  9. # All reports go into this directory
  10. reportsreports.dir=reports
  11. # Unit test reports from JUnit are deposited into this directory
  12. reports.xml.dir=${reports.dir}/junit-xml
  13. reports.html.dir=${reports.dir}/junit-html
  14. # Coverage reports are deposited into these directories
  15. coverage.xml.dir=${reports.dir}/cobertura-xml
  16. coverage.summaryxml.dir=${reports.dir}/cobertura-summary-xml
  17. coverage.html.dir=${reports.dir}/cobertura-html

转载于:https://blog.51cto.com/justlpf/1191253

Java Coverage(Cobertura)工具相关推荐

  1. java coverage_Java Coverage(Cobertura)工具

    首先是下载Cobertura的jar包了,这个工具底层是JCoverage,熟悉Jcoverage的对这个也不会陌生的. 大家可以了解很多东西,比如现在的作者啊什么,这里就不介绍了 然后点Downlo ...

  2. Java应用DevOps工具链推荐

    文章目录 Java应用DevOps工具链推荐 前言 Java应用DevOps工具链 扩展阅读 Java应用DevOps工具链推荐 前言 本文列出了Java应用常用的DevOps工具链,供大家参考. 涉 ...

  3. Java代码自动化测试工具Parasoft Jtest 2021全新发布,支持更多IDE

    Parasoft Jtest通过提供一套工具来加速Java软件开发,以保证您的软件可靠,安全和可维护,从而最大限度地提高质量并最大限度地降低业务风险.全面且可配置的报告使开发人员和管理人员能够理解并优 ...

  4. java安卓开发工具_推荐几个非常实用的Android开发工具

    工欲善其事,必先利其器.我们进行Android开发也要有好的开发工具辅助才能更好更高效的完成各种开发,为用户提供更实用的应用程序.本文就为大家推荐几个非常实用的Android开发工具,及几个常用的编辑 ...

  5. 分享五款java学习辅助工具,总有你用的上的~

    想要学好java技术,除了自身的努力,辅助工具也不缺少,辅助工具可以帮助大家在今后的工作中可以提高工作效率,下面小编就来分享五款java学习辅助工具,总有你用的上的~ 五款java学习辅助工具: 1. ...

  6. Java XML解析工具 dom4j介绍及使用实例

    Java XML解析工具 dom4j介绍及使用实例 dom4j介绍 dom4j的项目地址:http://sourceforge.net/projects/dom4j/?source=directory ...

  7. Java封装OkHttp3工具类

    点击关注公众号,Java干货及时送达  作者:如漩涡 https://blog.csdn.net/m0_37701381 Java封装OkHttp3工具类,适用于Java后端开发者 说实在话,用过挺多 ...

  8. 一些Java反编译工具/源代码查看工具的介绍

    2019独角兽企业重金招聘Python工程师标准>>> 有的朋友抱怨他们在使用他们公司的闭源框架时看不到底层的源代码.那么可以尝试使用一些Java反编译工具. 下面我举个例子介绍具体 ...

  9. 细说Java主流日志工具库

    细说 Java 主流日志工具库 日志框架 java.util.logging (JUL) Log4j Logback Log4j2 Log4j vs Logback vs Log4j2 日志门面 co ...

最新文章

  1. Linq 集合处理(Union)
  2. UA MATH567 高维统计II 随机向量2 各向同性的随机向量
  3. Windows Server Core管理之WinRM
  4. sentinel的@SentinelResource注解使用
  5. AtCoder Beginner Contest 192 F - Potion 背包dp
  6. 又一任务被Transformer攻陷!NVIDIA开源HORST,用Transformer解决早期动作识别和动作预期任务...
  7. Windows 2003超级终端
  8. Abobe Flash cs6 和 Abobe Flash Builder4.6 破解和许可证过期
  9. python 完全背包问题_完全背包问题及Python代码实现
  10. 获取U盘 VID,PID
  11. 现汇买入价/现钞买入价/卖出价/基准价/中行折算价的概念
  12. 西门子——不同数据的存储方式
  13. 三星android pie更新,三星Android Pie更新路线图公布 Galaxy Note9需等明年二月
  14. apmserv5.2.6 mysql启动失败_win7(xp) APMServ5.2.6 Apache启动失败,MYSQL启动失败 的解决办法...
  15. 微信小程序 图片旋转后上传
  16. redis incr命令最大值问题
  17. Fabric背书过程中链码是并行还是串行?
  18. X-Powered-By: Servlet/3.0漏洞修复
  19. 红帽第四季度订阅的强劲增长 整体表现超预期
  20. Oracle在中国的裁员暗示了中国的云服务市场什么?

热门文章

  1. python调用cmd命令会弹出黑框_python 调用cmd,不显示cmd黑框
  2. html css js实现快递单打印_html+css+js实现计算器
  3. 情人节 html5,情人节H5案例 | 2019第一波情人节营销已上线
  4. mimo的误码率_揭晓MU-MIMO黑科技!
  5. 电脑机时,电脑死机时,为啥会忍不住扇它一巴掌?
  6. ios share extension 真机不显示_ios企业签名:APPGroups实现App之间数据共享
  7. python特性和属性_Python之属性、特性和修饰符
  8. 使用SpringBoot发送邮件 在本地测试是好的 放到服务器连接超时问题
  9. Spring之注解方式实例化Java类
  10. ios技术篇-CoreData