首先是下载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. /**   
  4.  * @author cnchenhl 2011/02/25   
  5.  */   
  6. public class PrintStringOfYourName {   
  7.    
  8.     private String name = null;   
  9.     private int id = 0;   
  10.     private String sex = null;   
  11.     private int age = 0;   
  12.     private String address;   
  13.    
  14.     public String getName() {   
  15.         return name;   
  16.     }   
  17.    
  18.     public int getId() {   
  19.         return id;   
  20.     }   
  21.    
  22.     public String getSex() {   
  23.         return sex;   
  24.     }   
  25.    
  26.     public int getAge() {   
  27.         return age;   
  28.     }   
  29.    
  30.     public String getAddress() {   
  31.         return address;   
  32.     }   
  33.    
  34.     public void setName(String name) {   
  35.         this.name = name;   
  36.     }   
  37.    
  38.     public void setId(int id) {   
  39.         this.id = id;   
  40.     }   
  41.    
  42.     public void setSex(String sex) {   
  43.         this.sex = sex;   
  44.     }   
  45.    
  46.     public void setAge(int age) {   
  47.         this.age = age;   
  48.     }   
  49.    
  50.     public void setAddress(String address) {   
  51.         this.address = address;   
  52.     }   
  53.    
  54.     public String toString() {   
  55.         return "Name :" + name + "ID :" + id + "Age :" + age + "Sex :" + sex + "Address :" + address;   
  56.     }   
  57.    
  58. }   

 下面是测试的类test

Printstringofyournametest代码   收藏代码

 

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

 好了准备工作完成了

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

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

Build.properties代码   收藏代码

 

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