我正在尝试使用针对Jenkins的Cobertura插件获得代码覆盖率,因此我在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)时,它总是在说
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,好几天都没有运气了。我无法正常工作。拜托,我会很感谢您的帮助。这是我第一次使用蚂蚁(和Cobertura)
谢谢大家的回答。经过进一步调查,我弄清楚了如何使其工作。我必须完全重新编写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文件中