jenkins the definitive guide

(Jeff_L) #1
<target name="test-coverage" depends="instrument">
<junit fork="yes" dir="/Users/johnsmart/Projects/Books/jenkins-the-definitive-guide/hudsonbook-content">¶
<classpath location="${instrumented.dir}" />
<classpath location="${classes.dir}" />
<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>
</target>

¶ Run the JUnit tests against the instrumented application classes.
· The instrumented classes use Cobertura classes, so the Cobertura libraries also need to be on the
classpath.


This will produce the raw test coverage data we need to produce the XML test coverage reports that
Jenkins can use. To actually produce these reports, we need to invoke another task, as shown here:


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

Finally, don’t forget to tidy up after your done: the clean target should delete not only the generated
classes, but also the generated instrumented classes, the Cobertura coverage data, and the Cobertura
reports:


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

Once this is done, you are ready to integrate your coverage reports into Jenkins.


6.6.1.3. Installing the Cobertura code coverage plugin


Once code coverage data is being generated as part of your build process, you can configure Jenkins
to report on it. This involves installing the Jenkins Cobertura plugin. We went through this process
in Section 2.8, “Adding Code Coverage and Other Metrics”, but we’ll run through it again to refresh
your memory. Go to the Manage Jenkins screen, and click on Manage Plugins. This will take you to

Free download pdf