首先是下载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