I am trying to get code coverage using Cobertura plugin for Jenkins so I instrument, run tests and then coverage report in my build.xml as follow
<?xml version="1.0" encoding="UTF-8"?>
<project name="CoberturaAndJenkins" default="default" basedir=".">
<description>Builds, tests, and runs the project CoberturaAndJenkins.</description>
<import file="nbproject/build-impl.xml"/>
<property name="lib.dir" value="lib/" />
<property name="junit.file" value="junit-4.11.jar" />
<property name="cobertura.dir" value="${lib.dir}/cobertura/" />
<property name="instrumented.dir" value="/var/lib/jenkins/workspace/CoberturaAndJenkins/instrumented/" />
<property name="coveragereport.dir" value="build/cobertura/" />
<property name="classes.dir" value="/var/lib/jenkins/workspace/CoberturaAndJenkins/build/classes/" />
<property name="reports.xml.dir" value="build/test/results/" />
<!-- Modules that build -->
<property name="src.dir" value="src/" />
<property name="test.dir" value="test/" />
<!-- Define the Cobertura Ant tasks -->
<path id="cobertura.classpath">
<fileset dir="${cobertura.dir}">
<include name="cobertura-2.0.3.jar" />
<include name="lib/**/*.jar" />
</fileset>
<fileset dir="${lib.dir}" >
<include name="${junit.file}" />
</fileset>
</path>
<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
<path id="test.classpath">
<path refid="cobertura.classpath" />
<pathelement location="${bin}" />
<pathelement location="${instrumented.dir}"/>
<pathelement location="${classes.dir}" />
</path>
<!--============= Instrument the classes that JUnit will be testing =============-->
<target name="instrument">
<delete file="cobertura.ser"/>
<delete dir="${instrumented.dir}" />
<cobertura-instrument todir="${instrumented.dir}" datafile="cobertura.ser" >
<fileset dir="${classes.dir}">
<include name="**/*.class" />
<exclude name="**/*Test.class" />
</fileset>
</cobertura-instrument>
</target>
<!--======================= JUNIT =======================-->
<property name="basedir" value="./" />
<target name="module-test" depends="instrument">
<echo level="info" message="Running test cases..." />
<junit dir="${basedir}" showoutput="yes" fork="yes" printsummary="yes" failureProperty="test.failed">
<!--Specify the name of the coverage data file to use.
The value specified below is the default.-->
<sysproperty key="net.sourceforge.cobertura.datafile" file="cobertura.ser" />
<classpath refid="test.classpath" />
<formatter type="xml" />
<batchtest fork="yes" todir="${reports.xml.dir}">
<fileset dir="${test.dir}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>
</target>
<!--===================== COBERTURA REPORT ================================-->
<target name="coverage-report" depends="module-test">
<cobertura-report format="xml" destdir="${coveragereport.dir}" datafile="cobertura.ser" >
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
</cobertura-report>
<cobertura-report format="html" destdir="${coveragereport.dir}" datafile="cobertura.ser" >
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
</cobertura-report>
</target>
<!--================== END OF COBERTURA REPORT ============================-->
</project>
The thing is that when it tries to run the unit tests (which I can run using ant test
) it keeps saying
module-test:
Running test cases...
Running coberturaandjenkins.CoberturaAndJenkinsTest
Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
Test coberturaandjenkins.CoberturaAndJenkinsTest FAILED
I have changed, re-changed, re-re-changed the build.xml without any luck for days. I can't get it work. Please, I'd appreciate any help here. It's the first time I'm using ant (and Cobertura)