How do I generate Emma code coverage reports using Ant? - ant

How do I setup an Ant task to generate Emma code coverage reports?

To answer questions about where the source and instrumented directories are (these can be switched to whatever your standard directory structure is):
<property file="build.properties" />
<property name="source" location="src/main/java" />
<property name="test.source" location="src/test/java" />
<property name="target.dir" location="target" />
<property name="target" location="${target.dir}/classes" />
<property name="test.target" location="${target.dir}/test-classes" />
<property name="instr.target" location="${target.dir}/instr-classes" />
Classpaths:
<path id="compile.classpath">
<fileset dir="lib/main">
<include name="*.jar" />
</fileset>
</path>
<path id="test.compile.classpath">
<path refid="compile.classpath" />
<pathelement location="lib/test/junit-4.6.jar" />
<pathelement location="${target}" />
</path>
<path id="junit.classpath">
<path refid="test.compile.classpath" />
<pathelement location="${test.target}" />
</path>
First you need to setup where Ant can find the Emma libraries:
<path id="emma.lib" >
<pathelement location="${emma.dir}/emma.jar" />
<pathelement location="${emma.dir}/emma_ant.jar" />
</path>
Then import the task:
<taskdef resource="emma_ant.properties" classpathref="emma.lib" />
Then instrument the code:
<target name="coverage.instrumentation">
<mkdir dir="${instr.target}"/>
<mkdir dir="${coverage}"/>
<emma>
<instr instrpath="${target}" destdir="${instr.target}" metadatafile="${coverage}/metadata.emma" mode="copy">
<filter excludes="*Test*"/>
</instr>
</emma>
<!-- Update the that will run the instrumented code -->
<path id="test.classpath">
<pathelement location="${instr.target}"/>
<path refid="junit.classpath"/>
<pathelement location="${emma.dir}/emma.jar"/>
</path>
</target>
Then run a target with the proper VM arguments like:
<jvmarg value="-Demma.coverage.out.file=${coverage}/coverage.emma" />
<jvmarg value="-Demma.coverage.out.merge=true" />
Finally generate your report:
<target name="coverage.report" depends="coverage.instrumentation">
<emma>
<report sourcepath="${source}" depth="method">
<fileset dir="${coverage}" >
<include name="*.emma" />
</fileset>
<html outfile="${coverage}/coverage.html" />
</report>
</emma>
</target>

The User Guide has a good example of how to set up your build script so that you not only seperate the instrumented code from the execution, but it's also all contained in the same <target> so that you don't have to run a series of different targets, but instead you can just do something like ant emma tests (if ant tests was how you normally ran your unit tests, for example).
Here's their example:
<target name="emma" description="turns on EMMA instrumentation/reporting" >
<property name="emma.enabled" value="true" />
<!-- EMMA instr class output directory: -->
<property name="out.instr.dir" value="${basedir}/outinstr" />
<mkdir dir="${out.instr.dir}" />
</target>
<target name="run" depends="init, compile" description="runs the examples" >
<emma enabled="${emma.enabled}" >
<instr instrpathref="run.classpath"
destdir="${out.instr.dir}"
metadatafile="${coverage.dir}/metadata.emma"
merge="true"
/>
</emma>
<!-- note from matt b: you could just as easily have a <junit> task here! -->
<java classname="Main" fork="true" >
<classpath>
<pathelement location="${out.instr.dir}" />
<path refid="run.classpath" />
<path refid="emma.lib" />
</classpath>
<jvmarg value="-Demma.coverage.out.file=${coverage.dir}/coverage.emma" />
<jvmarg value="-Demma.coverage.out.merge=true" />
</java>
<emma enabled="${emma.enabled}" >
<report sourcepath="${src.dir}" >
<fileset dir="${coverage.dir}" >
<include name="*.emma" />
</fileset>
<txt outfile="${coverage.dir}/coverage.txt" />
<html outfile="${coverage.dir}/coverage.html" />
</report>
</emma>
</target>

Emma 2.1 introduces another way of obtaining runtime coverage information (.ec file). One can remotely request the data from the given port of the computer where an instrumented application is runnig. So there's no need to stop VM.
To get the file with runtime coverage data you need to insert the following snippet in your Ant script between running of your tests and generating coverage report:
<emma>
<ctl connect="${emma.rt.host}:${emma.rt.port}" >
<command name="coverage.get" args="${emma.ec.file}" />
<command name="coverage.reset" />
</ctl>
</emma>
Other steps are similar to Emma 2.0. They are perfectly described in previous post
More information on Emma 2.1 features: http://sourceforge.net/project/shownotes.php?group_id=108932&release_id=336859

Related

How to setup jacoco java agent in wildfly

I have to setup jacoco with wildfly.
i have provided java opts in standalone.bat.
i have jcoco ant tasks for coverage and report.
i am getting test code coverage always 0%.
my junits extend cactus servertest.
if i do not extend servertest(cactus) i will get code coverage as 100% else 0%.
In the sessions.html i do not find classes.
standalone.bat configuration.
http://www.eclemma.org/jacoco/trunk/doc/agent.html
jcoco ant tasks.
http://www.eclemma.org/jacoco/trunk/doc/ant.html
standalone.bat
set "JAVA_OPTS=-Dprogram.name=%PROGNAME% %JAVA_OPTS% -javaagent:{myhome.dir}/jacocoagent.jar=destfile={myhome.dir}/jacoco.exec,excludes=${jboss.home.dir}/modules/**/*,classdumpdir={myhome.dir}/dump,dumponexit=false,append=false"
ant tasks:
<target name="test">
<delete file="jacoco.exec" />
<delete dir="${project.dir}/junit" />
<mkdir dir="${project.dir}/junit/result" />
<mkdir dir="${project.dir}/junit/report" />
<jacoco:coverage>
<junit fork="true" maxmemory="512M" printsummary="true" haltonerror="false" haltonfailure="false" showoutput="yes">
<jvmarg line="${cactus.args}" />
<classpath>
<path refid="build.classpath1" />
</classpath>
<formatter type="xml" />
<batchtest todir="${project.dir}/junit/result">
<fileset dir="{JBOSS.HOME}\standalone\deployments\flexnet.ear\flexnet.war\WEB-INF\classes" includes="**/DumyTest*" excludes="${test.exclude.files}" />
</batchtest>
</junit>
</jacoco:coverage>
<junitreport tofile="TestResults.xml" todir="${project.dir}/junit/result">
<fileset dir="${project.dir}/junit/result" />
<report format="frames" todir="${project.dir}/junit/report" />
</junitreport>
</target>
<target name="test-coverage-report">
<delete dir="${project.dir}/code-coverage" />
<jacoco:report>
<executiondata>
<file file="jacoco.exec" />
</executiondata>
<structure name="Code Coverage Results">
<group name="FNO">
<classfiles>
<fileset dir="{JBOSS.HOME}\standalone\deployments\flexnet.ear\flexnet.war\WEB-INF\classes" />
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}" />
<fileset dir="${testsrc.dir}" />
</sourcefiles>
</group>
</structure>
<html destdir="${project.dir}/code-coverage" />
</jacoco:report>
</target>
Any insight on this issue.please
it was type error.
Just Adding *
* helped me to resolve issue.
When i looked classdump i found classes which were not required.
when i added includes only required classes were added.

TestNg/Selenium call by ant always return Cannot find class in the classpath

I'm pretty new with this setup. And having issue to call my project with TestNG by ant.
I can run the testng.xml without any problem in Eclipse but I alway receive Cannot find class in classpath by ant.
Build.xml
<project basedir="." default="runTest" name="Ant file for TestNG">
<property name="src" location="src" />
<property name="bin" location="bin" />
<property name="telus" location="C:\ESP_Testware\ESP_Projects\Selenium\telus-pharma-integration-tests\src\test\resources\suite\local" />
<property name="libs" location="lib" />
<path id="class.path">
<pathelement location="${libs}/testng-6.4.jar" />
<pathelement location="${libs}/selenium-java-client-driver.jar" />
<pathelement location="${libs}/selenium-server-standalone-2.39.0.jar" />
<pathelement location="${bin}"/>
<pathelement location="${telus}"/>
</path>
<taskdef name="testng" classname="org.testng.TestNGAntTask">
<classpath>
<pathelement location="${libs}/testng-6.4.jar"/>
</classpath>
</taskdef>
<target name="runTest">
<echo message="mkdir"/>
<mkdir dir="testng_output"/><!-- Create the output directory. -->
<echo message= "TestNg Start"/>
<testng outputdir="testng_output" classpathref="class.path">
<xmlfileset dir="${telus}" includes="testng.xml"/>
<!-- <xmlfileset dir="." includes="TestNG2.xml"/> -->
</testng>
</target>
</project>
Testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Bolt harness QA" verbose="1">
<parameter name="test.env" value="qa" />
<parameter name="selenium.url" value="https://www.google.com" />
<!-- Valid values for browser: FF, IE, Chrome -->
<parameter name="selenium.browser" value="Chrome" />
<listeners>
<listener class-name="com.gdo.test.integration.listener.SoftAssertTestListener" />
</listeners>
<test name="Test_MS_Website" preserve-order="true">
<classes>
<class name="com.gdo.telus.SC006">
<methods>
<include name="Web_InvalidPassword" />
<exclude name="Web_LockedAccount" />
</methods>
</class>
</classes>
</test>
</suite>
My Class are at this path :
C:\ESP_Testware\ESP_Projects\Selenium\telus-pharma-integration-tests\src\test\java\com\gdo\telus
Thanks for your help.
Try my build.xml file, I did add the ReportNG plugin into this build.xml file to generate better looking reports instead of the default TestNG reports. You can just download the jar file for ReportNG and place it into your lib folder and it should still work fine:
<project name="Some Bullshit Goes Here" default="clean" basedir=".">
<!-- Initilization properties -->
<!-- <property name="lib.dir" value="${basedir}/lib"/> -->
<!-- using the ${basedir} allows you to use relative paths. It will use the working directory and add folders that you specify -->
<property name="build.dir" value="${basedir}/build"/>
<property name="lib.dir" value="hardcoded value can go here"/>
<property name="src.dir" value="${basedir}/src"/>
<property name="bin.dir" value="${basedir}/bin"/>
<property name="output.dir" value="${basedir}/output"/>
<!-- I chose to hardcode the location where my jar library files will be, it will be used for compilation. Again you can set relative path if you wish.-->
<path id="assloadoflibs">
<fileset dir="/automated/tests/library">
<include name="*.jar"/>
</fileset>
<pathelement path="${basedir}/bin"/>
</path>
<!-- setting libraries -->
<target name="setClassPath">
<path id="classpath_jars">
<pathelement path="${basedir}/"/>
<fileset dir="/automated/tests/library" includes="*.jar"/>
</path>
<!-- Convert jar collection from a given reference into one list, storing the result into a given property, separated by colon -->
<pathconvert pathsep=":" property="test.classpath" refid="classpath_jars"/>
</target>
<target name="loadTestNG" depends="setClassPath">
<!-- Creating task definition for TestNG task -->
<taskdef resource="testngtasks" classpath="${test.classpath}"/>
</target>
<target name="init">
<!-- Creating build directory structure used by compile -->
<mkdir dir="${build.dir}"/>
</target>
<target name="clean">
<echo message="deleting existing build directory"/>
<delete dir="${build.dir}"/>
</target>
<!-- In compile target dependency is given over clean target followed by init,
this order makes sure that build directory gets created before compile takes place
This is how a clean complile is achieved.
-->
<target name="compile" depends="clean,init,setClassPath,loadTestNG">
<echo message="classpath:${test.classpath}"/>
<echo message="compiling..."/>
<javac destdir="${build.dir}" srcdir="${src.dir}" classpath="${test.classpath}"/>
</target>
<target name="run" depends="compile">
<!-- testng classpath has been provided reference of jar files and compiled classes
this will generate report NG report.
-->
<testng classpath="${test.classpath}:${build.dir}" outputdir="${basedir}/output" haltonfailure="false" useDefaultListeners="true" listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter" classpathref="reportnglibs">
<xmlfileset dir="${basedir}" includes="testng.xml"/>
<!-- This value here will show the title of the report -->
<sysproperty key="org.uncommons.reportng.title" value="Example Test Report"/>
</testng>
</target>
</project>
Here is my TestNG.xml file:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Example Test Suite">
<test name ="Example TestCase Name">
<classes>
<class name="packageName.JavaFilename"></class>
</classes>
</test>
</suite>
I've found my answer on this site. I need to use maven to call my solution.
http://rationaleemotions.wordpress.com/2012/05/14/continuous-integration-with-selenium/
but thanx anyway for your help

Use of Emma for JUnit in ant build.xml

I am newbie to use Emma. I am trying to add emma ant task for JUnit test case for modules in EAR project. I have few question here.
Should I use instrumented class for packaging my EAR projet?
What is good way to add emma ant task for junit? Should I use emmarun:on-th-fly mode or offline mode? For JUnit should I use fork or no fork?
I am using Emma Offline mode and Junit with fork. Here is my build.xml
<!--Target and task for EMMA -->
<taskdef resource="emma_ant.properties" classpathref="Emma.libraryclasspath" />
<target name="emma" description="turns on EMMA's instrumentation/reporting" >
<property name="emma.enabled" value="true" />
<mkdir dir="${out.instr.dir}" />
<property name="emma.filter" value="" />
</target>
<target name="test" depends="init, compile" description="Run JUnit Test cases under emma environment">
<!-- Emma instrumentation -->
<emma enabled="${emma.enabled}" verbosity="verbose">
<instr instrpath="${class.dir}"
destdir="${out.instr.dir}"
metadatafile="${coverage.dir}/metadata.em"
merge="true"
mode="copy">
<filter value="${emma.filter}" />
</instr>
</emma>
<!-- JUnit Start -->
<junit printsummary="yes" fork="yes">
<test name="com.hf.platform.authorizer.WebTxnAuthorizerTest" todir="${test.report.dir}">
<formatter type="xml"/>
</test>
<classpath>
<path refid="HFPlatformWeb.classpath"/>
<path refid="Emma.libraryclasspath"/>
</classpath>
<jvmarg value="-Demma.coverage.out.file=${coverage.dir}/coverage.ec" />
<jvmarg value="-Demma.coverage.out.merge=false" />
</junit>
<!-- Junit End -->
<emma enabled="${emma.enabled}" verbosity="verbose">
<report>
<sourcepath>
<dirset dir="${basedir}">
<include name="src"/>
<include name="test-src"/>
</dirset>
</sourcepath>
<fileset dir="${coverage.dir}">
<include name="*.em"/>
<include name="*.ec"/>
</fileset>
<xml outfile="${coverage.report.dir}/report.xml" />
<txt outfile="${coverage.report.dir}/report.txt" />
<html outfile="${coverage.report.dir}/report.html" />
</report>
</emma>
</target>
When I ran it for one test, it is not generating any report. But when i ran same unit test with EclEmma it gives correct output.
In above example we need to make sure following two things
The file path for metadatafile and coverage report file that is .ec, .em or .emma file should be absolute or relative to project.
e.g.
For running java/junit task sandwiched between the instrumentation and report task, it must use instrumented class file path.
e.g.
<classpath> <pathelement location="${out.instr.dir}" />
<path refid="Emma.libraryclasspath"/>
<path refid="HFPlatformEJB.classpath"/>
</classpath>

sonar ant task or hudson intergration for coverage report

Good morning, i've got a question about cobertura report integration in sonar.
I've tried two way: from hudson and from ant task.
if i used hudson to configure a sonar integration, the cobertura report is well integrated to my sonar dashboard but i've got some warning like
Java bytecode scan...
[WARN] Class 'javax/mail/Folder' is not accessible through the ClassLoader.
[WARN] Class 'javax/mail/Folder' is not accessible through the ClassLoader.
[WARN] Class 'javax/mail/Folder' is not accessible through the ClassLoader.
[WARN] Class 'javax/mail/Folder' is not accessible through the ClassLoader.
[WARN] Class 'com/sun/mail/imap/IMAPFolder' is not accessible through the ClassLoader.
But, my cobertura report is well integrated:
[INFO] Sensor CoberturaSensor...
[INFO] parsing C:\hudson\jobs\Client Mail\workspace\cobertura\reports\coverage.xml
[INFO] Sensor CoberturaSensor done: 1218 ms
So, i decide to use ant task so i can manage my librairies, this is my ant task:
<target name="sonar" depends="cover-report">
<!-- The workDir directory is used by Sonar to store temporary files -->
<sonar:sonar workDir="./" key="fr.simscorps:client.mail" version="0.1-SNAPSHOT" xmlns:sonar="antlib:org.sonar.ant">
<property key="sonar.host.url" value="http://172.30.3.55:1234/sonar/" />
<!-- source directories (required) -->
<sources>
<path location="./src" />
</sources>
<!-- list of properties (optional) -->
<property key="sonar.dynamicAnalysis" value="reuseReports" />
<property key="sonar.projectName" value="client mail" />
<property key="sonar.java.source" value="1.5" />
<property key="sonar.projectVersion" value="0.1-SNAPSHOT" />
<property key="sonar.phase" value="generate-sources"/>
<property key="sonar.cobertura.reportsPath" value="${reports.xml.dir}/coverage.xml"/>
<property key="sonar.surefire.reportsPath" value="${reports.xml.dir}/TESTS-test.TestSuiteClientMail.xml" />
<!-- test source directories (optional) -->
<tests>
<path location="./test" />
</tests>
<!-- binaries directories, which contain for example the compiled Java bytecode (optional) -->
<binaries>
<path location="./bin" />
</binaries>
<!-- path to libraries (optional). These libraries are for example used by the Java Findbugs plugin -->
<libraries>
<path location="./lib/activation.jar" />
<path location="./lib/deltasync.jar" />
<path location="./lib/jaybird_full_2_1_6.jar" />
<path location="./lib/junit.jar" />
<path location="./lib/mail.jar" />
<path location="./lib/libdeltasync/apache-mime4j-0.5.jar"/>
<path location="./lib/libdeltasync/commons-codec-1.5.jar"/>
<path location="./lib/libdeltasync/commons-codec-1.5-javadoc.jar"/>
<path location="./lib/libdeltasync/commons-codec-1.5-sources.jar"/>
<path location="./lib/libdeltasync/commons-logging-1.1.1.jar"/>
<path location="./lib/libdeltasync/httpclient-4.1.1.jar"/>
<path location="./lib/libdeltasync/httpclient-cache-4.1.1.jar"/>
<path location="./lib/libdeltasync/httpcore-4.1.jar"/>
<path location="./lib/libdeltasync/httpmime-4.1.1.jar"/>
<path location="./lib/libdeltasync/logback-classic-0.9.29.jar"/>
<path location="./lib/libdeltasync/logback-classic-0.9.29-sources.jar"/>
<path location="./lib/libdeltasync/logback-core-0.9.29.jar"/>
<path location="./lib/libdeltasync/logback-core-0.9.29-sources.jar"/>
<path location="./lib/libdeltasync/slf4j-api-1.6.1.jar"/>
<path location="./lib/libdeltasync/slf4j-api-1.6.1-sources.jar"/>
</libraries>
</sonar:sonar>
</target>
My report is well generated (about 700ko) and in hudson's log i see the cobertura sensor, but it's like he didn't do anything:
[sonar:sonar] [INFO] Sensor CoberturaSensor...
[sonar:sonar] [INFO] Sensor CoberturaSensor done: 0 ms
Here, my other ant task dependency:
<property name="junit.output.dir" value="junit"/>
<property name="instrumented.dir" value="cobertura/instrumented"/>
<property name="classes.dir" value="bin"/>
<property name="jars.dir" value="lib"/>
<property name="test.dir" value="test"/>
<property name="cobertura.dir" value="${jars.dir}/cobertura"/>
<property name="reports.xml.dir" value="cobertura/reports"/>
<property name="cob.ser.file" value="cobertura.ser" />
<!-- Define the Sonar task if this hasn't been done in a common script -->
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="C:\hudson\tools\my_ant\lib" />
</taskdef>
<path id="cobertura.classpath">
<fileset dir="${cobertura.dir}">
<include name="cobertura.jar" />
<include name="lib/**/*.jar" />
</fileset>
<fileset dir="${jars.dir}">
<include name="**/*.jar"/>
</fileset>
</path>
<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
<target name="clean" >
<delete dir="${instrumented.dir}" />
<delete dir="${reports.xml.dir}" />
<delete file="${cob.ser.file}" />
</target>
<target name="prepare" depends="clean">
<mkdir dir="${instrumented.dir}" />
<mkdir dir="${reports.xml.dir}" />
</target>
<target name="instrument" depends="prepare">
<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="testsuite" depends="instrument">
<junit fork="yes" dir="${test.dir}" failureProperty="test.failed" printsummary="withOutAndErr">
<!--
Specify the name of the coverage data file to use.
The value specified below is the default.
-->
<sysproperty key="net.sourceforge.cobertura.datafile"
file="${basedir}/cobertura.ser" />
<!--
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="test.TestSuiteClientMail" todir="${reports.xml.dir}" />
<!--<batchtest todir="${reports.xml.dir}" unless="testcase">
<fileset dir="${test.dir}">
<include name="**/*Test*.java" />
</fileset>
</batchtest>-->
</junit>
</target>
<target name="cover-report" depends="testsuite">
<cobertura-report format="xml" destdir="${reports.xml.dir}" srcdir="${test.dir}" />
</target>
Does anyone have an idea?
[ANSWER]
Ok, my fault, the problem is in syntax i used: it's
<property key="sonar.cobertura.reportPath" value="${reports.xml.dir}/coverage.xml"/>
and not
<property key="sonar.cobertura.reportsPath" value="${reports.xml.dir}/coverage.xml"/> as we can see here sorry
As for the Class Loader errors you are getting, I got rid of mine by making sure the paths in my binaries & libraries property are made from the root of my project.
<property name="sonar.libraries" value="./Module/resources/web/WEB-INF/lib" />
rather than
<property name="sonar.libraries" value="resources/web/WEB-INF/lib" />
Which gives the class loader error. Hope this helps someone. Took my a few days to figure it out!

Cobertura generates my reports for code coverage, but the coverage shows as 0%

All of my reports get generated, but my coverage shows as 0%. I even created one dummy test to make sure it wasn't the way my tests were written, and it doesn't show for the one dummy class I'm covering. Here is my Ant build for this:
<?xml version="1.0" encoding="UTF-8" ?>
<project name="My Project Name" default="run.cobertura" basedir=".">
<description>My Description</description>
<!-- target: init -->
<target name="init">
<!-- create properties and directory structure -->
<property name="src.path" value="${basedir}/src" />
<property name="lib.path" value="${basedir}/lib" />
<property name="output.path" value="${basedir}/bin" />
<property name="testcase-unit-only.path" value="${basedir}/testcase-unit-only" />
<property name="testcase-unit-only.output.path" value="${basedir}/test-classes" />
<property name="cobertura.lib.path" value="${basedir}/lib-cobertura" />
<property name="cobertura.path" value="${basedir}/cobertura" />
<property name="cobertura.output.path" value="${cobertura.path}/bin" />
<property name="cobertura.reports.path" value="${cobertura.path}/reports" />
<property name="cobertura.data.file" value="${cobertura.path}/cobertura.ser" />
<delete dir="${testcase-unit-only.output.path}" />
<delete dir="${cobertura.path}"/>
<mkdir dir="${testcase-unit-only.output.path}"/>
<mkdir dir="${cobertura.path}"/>
<mkdir dir="${cobertura.output.path}"/>
<!-- define classpath references -->
<path id="cp.lib.path">
<fileset dir="${lib.path}">
<include name="*.jar"/>
</fileset>
</path>
<path id="cp.classes.path">
<pathelement path="${output.path}" />
</path>
<path id="cp.classes.test.path">
<pathelement path="${testcase-unit-only.output.path}" />
</path>
<path id="cp.lib.cobertura.path">
<fileset dir="${cobertura.lib.path}">
<include name="*.jar"/>
</fileset>
</path>
<path id="cp.all.path">
<path refid="cp.lib.path"/>
<path refid="cp.classes.path"/>
<path refid="cp.lib.cobertura.path"/>
</path>
</target>
<!-- target: run.cobertura-instrument -->
<target name="run.cobertura-instrument">
<taskdef classpathref="cp.lib.cobertura.path" resource="tasks.properties"/>
<cobertura-instrument todir="${cobertura.output.path}" datafile="${cobertura.data.file}">
<fileset dir="${output.path}">
<include name="**/*.class" />
</fileset>
</cobertura-instrument>
</target>
<!-- target: compile.classes -->
<target name="compile.classes">
<javac srcdir="${src.path}" destdir="${output.path}">
<classpath refid="cp.lib.path"/>
</javac>
</target>
<!-- target: compile.tests -->
<target name="compile.tests">
<javac srcdir="${testcase-unit-only.path}" destdir="${testcase-unit-only.output.path}">
<classpath refid="cp.all.path"/>
</javac>
</target>
<!-- target: run.junit -->
<target name="run.junit">
<junit fork="true" dir="${basedir}" failureProperty="test.failed">
<classpath location="${cobertura.output.path}"/>
<classpath location="${output.path}"/>
<sysproperty key="net.sourceforge.cobertura.datafile" file="${cobertura.data.file}" />
<classpath refid="cp.lib.path"/>
<classpath refid="cp.classes.test.path"/>
<classpath refid="cp.lib.cobertura.path"/>
<formatter type="xml" />
<!-- <formatter type="brief" usefile="false"/> -->
<batchtest todir="${testcase-unit-only.output.path}" unless="testcase">
<fileset dir="${testcase-unit-only.output.path}">
<include name="**/*UnitTest.java"/>
</fileset>
</batchtest>
</junit>
</target>
<!-- target: run.cobertura -->
<target name="run.cobertura" depends="init,run.cobertura-instrument,compile.classes,compile.tests,run.junit">
<cobertura-report srcdir="src" destdir="${cobertura.reports.path}" datafile="${cobertura.data.file}"/>
</target>
</project>
One thing I notice is that in the depends list for the run.cobertura target you instrument the compiled classes before you compile them. That might work if you run twice, assuming the compiled classes from the first run are not cleared down, but doesn't seem quite right. On the first run if there are no instrumented classes, your report would be empty.
I had the same ant build scripts. It worked on my local workstation, but didn't on jenkins server.
But server had jdk 7 and workstation jdk6. After changing jdk on jenkins server to jdk6, code coverage generates without any problem.
you must set debug on when you compile java
<javac **debug="on"** srcdir="${testcase-unit-only.path}" destdir="${testcase-unit-only.output.path}">

Resources