我正在尝试使用Cobertura插件为Jenkins获取代码覆盖率,因此我在build.xml中运行测试然后覆盖报告,如下所示
<?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>
问题在于,当它试图运行单元测试时(我可以使用ant test
运行),它一直在说
module-test:
Running test cases...
Running coberturaandjenkins.CoberturaAndJenkinsTest
Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
Test coberturaandjenkins.CoberturaAndJenkinsTest FAILED
我已经更改,重新更改,重新更改build.xml几天没有任何运气。我无法让它发挥作用。拜托,我很感激这里的任何帮助。这是我第一次使用ant(和Cobertura)
答案 0 :(得分:2)
感谢你们的所有答案。经过进一步调查,我想出了如何使其发挥作用。我不得不完全重写build.xml。这是:
<property file="build.properties" />
<path id="cobertura.classpath">
<fileset dir="${cobertura.dir}">
<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" includeantruntime="false">
<classpath refid="cobertura.classpath" />
</javac>
<javac srcdir="${test.src.dir}" destdir="${classes.dir}" debug="yes" includeantruntime="false">
<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="${test.src.dir}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>
<!-- JUnit Report in HTML -->
<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"/>
</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."/>
属性定义位于开头引用的build.properties文件中