How to run Cobertura as an ant build script from eclipse

Cobertura unlike eCobertura plugin, provides advanced code coverage reporting in xml and html formats. This is much more flexible for custom configuration as it can be controlled via the ant script. Replicate the steps below :

1) Download cobertura zip.

2) Create a java/groovy project named "Sample" in eclipse.

3) Create a simple java/groovy class and a corresponding unit testcase class to test it.. Place them in seperate or same packages as you wish.

4) Create a folder called lib under the Sample project folder (as Sample/lib).. Place junit.jar, apache ant.jar, and all the jars in the downloaded Cobertura zip (most importantly cobertura.jar) into the lib folder and include the jars in the project's classpath.

5) From the downloaded Cobertura zip, pick up the two files build.properties and build.xml and place them in your Sample project base. (as Sample/build.xml and Sample.build.properties)

6) The build.xml is the ant script to run Cobertura and it uses the configuration in build.properties file.

7) Change the contents of build.properties as shown below:

build.properties
---------------------------------------------------------------------------------------------------------------------


# The source code for the examples can be found in this directory
src.dir=src

# The path to cobertura.jar
cobertura.dir=cobertura/complete

# Classes generated by the javac compiler are deposited in this directory
classes.dir=cobertura/complete/classes  

# Instrumented classes are deposited into this directory
instrumented.dir=cobertura/complete/instrumented

# All reports go into this directory
reports.dir=cobertura/complete/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

---------------------------------------------------------------------------------------------------------------------


8) Now change the contents of build.xml as shown below:


build.xml 

---------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>

<project name="Sample" default="coverage" basedir=".">

 <description>
    Cobertura - http://cobertura.sourceforge.net/
    Copyright (C) 2003 jcoverage ltd.
    Copyright (C) 2005 Mark Doliner &lt;thekingant@users.sourceforge.net&gt;
    Copyright (C) 2006 Dan Godfrey
    Cobertura is licensed under the GNU General Public License
    Cobertura comes with ABSOLUTELY NO WARRANTY
    </description>

 <property file="build.properties" />

 <path id="cobertura.classpath">
      <fileset dir="lib">
           <include name="*.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}" />
 </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">
  <!--
   Remove the coverage data file and any old instrumentation.
  -->
  <delete file="cobertura.ser"/>
  <delete dir="${instrumented.dir}" />
  <!--
   Instrument the application classes, writing the
   instrumented classes into ${build.instrumented.dir}.
  -->
  <cobertura-instrument todir="${instrumented.dir}">
   <!--
    The following line causes instrument to ignore any
    source line containing a reference to log4j, for the
    purposes of coverage reporting.
   -->
   <ignore regex="org.apache.log4j.*" />
   <fileset dir="${classes.dir}">
    <!--
     Instrument all the application classes, but
     don't instrument the test classes.
    -->
    <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">
   <!--
    Note the classpath order: instrumented classes are before the
    original (uninstrumented) classes.  This is important.
   -->
   <classpath location="${instrumented.dir}" />
   <classpath location="${classes.dir}" />
   <!--
    The instrumented classes reference classes used by the
    Cobertura runtime, so Cobertura and its dependencies
    must be on your 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">

  <!--

   Generate an XML file containing the coverage data using

   the "srcdir" attribute.

  -->

  <cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />

 </target>



 <target name="summary-coverage-report">

  <!--

   Generate an summary XML file containing the coverage data using

   the "srcdir" attribute.

  -->

  <cobertura-report srcdir="${src.dir}" destdir="${coverage.summaryxml.dir}" format="summaryXml" />

 </target>



 <target name="alternate-coverage-report">

  <!--

   Generate a series of HTML files containing the coverage

   data in a user-readable form using nested source filesets.

  -->

  <cobertura-report destdir="${coverage.html.dir}">

   <fileset dir="${src.dir}">

    <include name="**/*.java"/>

    <include name="**/*.groovy"/>

   </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" />

 </target>



 <target name="coverage" depends="compile,instrument,test,coverage-report,summary-coverage-report,alternate-coverage-report" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports."/>



</project>

---------------------------------------------------------------------------------------------------------------------

9) Running the above build.xml as an ant script in eclipse will create a folder hierarchy. The generated xml and html format reports can be found at Sample/cobertura/complete folder.

PS: Instead of running the script from eclipse you can also run the build.xml from a command line using the ant -[options] command.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值