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