我正在尝试使用Eclipse Neon中的Ant同时运行JUnit和JaCoCo。首先,我尝试使用JUnit运行所有测试,并使用自定义HTML文档进行报告:
<target name="AllDAOTests">
<mkdir dir="${junit.output.dir}"/>
<junit fork="yes" printsummary="withOutAndErr">
<formatter type="xml"/>
<!-- List of test -->
<test name="it.********.dao.DeviceDAOTest" todir="${junit.output.dir}"/>
<test name="it.********.dao.ScheduleActivityDAOTest" todir="${junit.output.dir}"/>
<jvmarg line="-ea"/>
<classpath refid="run.AllDAOTests.classpath"/>
</junit>
</target>
<!-- JUNIT -->
<target name="junitreport">
<!- Generates the report
<!- Specifies the directory to which the merged XML file is to be saved
<junitreport todir="${junit.output.dir}">
<fileset dir="${junit.output.dir}">
<include name="TEST-*.xml"/>
</fileset>
<!- Added report style (styledir="junit/reportstyle")
<report styledir="src/test/resources" format="noframes" todir="${junit.output.dir}"/>
</junitreport>
</target>
一切正常。我的下一步是从JaCoCo获得一份有关测试所用代码的报告。我尝试过这样的事情:
<target name="test" depends="compile">
<mkdir dir="${result.report.dir}"/>
<jacoco:coverage destfile="${result.exec.file}">
<junit printsummary="yes" haltonfailure="off" fork="yes">
<classpath refid="run.AllDAOTests.classpath"/>
<formatter type="xml"/>
<batchtest todir="${result.report.dir}">
<fileset dir="target/test-classes">
<include name="**/*Test.class"/>
</fileset>
</batchtest>
</junit>
</jacoco:coverage>
<junitreport todir="${junit.output.dir}">
<fileset dir="${junit.output.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report styledir="src/test/resources" format="noframes" todir="${junit.output.dir}"/>
</junitreport>
</target>
仅在构建时运行Ant,以正确的方式测试我得到的测试报告的结果
[jacoco:coverage] Enhancing junit with coverage
[junit] Running it.********.dao.DeviceDAOTest
[junit] Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 10,106 sec
[junit] Running it.********.dao.ScheduleActivityDAOTest
[junit] Tests run: 14, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 10,068 sec
[junitreport] Processing C:\Users\lubrano\Development\gitrepos\ECWebAppLocalRepo\ecwebapp\target\test-report\TESTS-TestSuites.xml to C:\Users\lubrano\Development\gitrepos\ECWebAppLocalRepo\ecwebapp\target\test-report\junit-noframes.html
[junitreport] Loading stylesheet C:\Users\lubrano\Development\gitrepos\ECWebAppLocalRepo\ecwebapp\src\test\resources\junit-noframes.xsl
[junitreport] Transform time: 508ms
BUILD SUCCESSFUL
Total time: 25 seconds
当我尝试HTML报告代码覆盖率并使用build,test,coverage运行Ant时出现问题:
<target name="coverage" depends="test">
<jacoco:report>
<executiondata>
<file file="${result.exec.file}"/>
</executiondata>
<structure name="Code Annotation Project">
<classfiles>
<fileset dir="${result.classes.dir}"/>
<fileset dir="target/test-classes"/>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="src/main/java"/>
<!-- <fileset dir="src/test/java"/> -->
</sourcefiles>
</structure>
<html destdir="${result.report.dir}"/>
</jacoco:report>
</target>
在两个测试用例中都开始出现此类错误:
[junit] Running it.********.dao.ScheduleActivityDAOTest
[junit] java.lang.instrument.IllegalClassFormatException: Error while instrumenting org/apache/tools/ant/util/DOMElementWriter.
[junit] Tests run: 14, Failures: 0, Errors: 7, Skipped: 0, Time elapsed: 13,851 sec
[junit] at org.jacoco.agent.rt.internal_c13123e.CoverageTransformer.transform(CoverageTransformer.java:93)
[junit] at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
[junit] at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428)
[junit] at java.lang.ClassLoader.defineClass1(Native Method)
[junit] at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
[junit] at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
[junit] at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
[junit] at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
[junit] at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
[junit] at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
[junit] at java.security.AccessController.doPrivileged(Native Method)
[junit] at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[junit] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter.endTestSuite(XMLJUnitResultFormatter.java:197)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.fireEndTestSuite(JUnitTestRunner.java:843)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:578)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1182)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:1033)
[junit] Caused by: java.io.IOException: Error while instrumenting org/apache/tools/ant/util/DOMElementWriter.
[junit] at org.jacoco.agent.rt.internal_c13123e.core.instr.Instrumenter.instrumentError(Instrumenter.java:173)
[junit] at org.jacoco.agent.rt.internal_c13123e.core.instr.Instrumenter.instrument(Instrumenter.java:123)
[junit] at org.jacoco.agent.rt.internal_c13123e.CoverageTransformer.transform(CoverageTransformer.java:91)
[junit] ... 19 more
[junit] Caused by: java.lang.IllegalStateException: Class org/apache/tools/ant/util/DOMElementWriter is already instrumented.
[junit] at org.jacoco.agent.rt.internal_c13123e.core.internal.instr.InstrSupport.assertNotInstrumented(InstrSupport.java:176)
[junit] at org.jacoco.agent.rt.internal_c13123e.core.internal.instr.ClassInstrumenter.visitField(ClassInstrumenter.java:55)
[junit] at org.jacoco.agent.rt.internal_c13123e.asm.ClassVisitor.visitField(ClassVisitor.java:294)
[junit] at org.jacoco.agent.rt.internal_c13123e.asm.ClassReader.readField(ClassReader.java:883)
[junit] at org.jacoco.agent.rt.internal_c13123e.asm.ClassReader.accept(ClassReader.java:694)
[junit] at org.jacoco.agent.rt.internal_c13123e.asm.ClassReader.accept(ClassReader.java:500)
[junit] at org.jacoco.agent.rt.internal_c13123e.core.instr.Instrumenter.instrument(Instrumenter.java:101)
[junit] at org.jacoco.agent.rt.internal_c13123e.core.instr.Instrumenter.instrument(Instrumenter.java:121)
[junit] ... 20 more
[junit] java.lang.instrument.IllegalClassFormatException: Error while instrumenting org/apache/tools/ant/util/DOMElementWriter$XmlNamespacePolicy.
[junit] at org.jacoco.agent.rt.internal_c13123e.CoverageTransformer.transform(CoverageTransformer.java:93)
[junit] at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
[junit] at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428)
[junit] at java.lang.ClassLoader.defineClass1(Native Method)
[junit] at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
[junit] at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
[junit] at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
[junit] at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
[junit] at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
[junit] at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
[junit] at java.security.AccessController.doPrivileged(Native Method)
[junit] at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[junit] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[junit] at org.apache.tools.ant.util.DOMElementWriter.<init>(DOMElementWriter.java:66)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter.endTestSuite(XMLJUnitResultFormatter.java:197)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.fireEndTestSuite(JUnitTestRunner.java:843)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:578)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1182)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:1033)
[junit] Caused by: java.io.IOException: Error while instrumenting org/apache/tools/ant/util/DOMElementWriter$XmlNamespacePolicy.
[junit] at org.jacoco.agent.rt.internal_c13123e.core.instr.Instrumenter.instrumentError(Instrumenter.java:173)
[junit] at org.jacoco.agent.rt.internal_c13123e.core.instr.Instrumenter.instrument(Instrumenter.java:123)
[junit] at org.jacoco.agent.rt.internal_c13123e.CoverageTransformer.transform(CoverageTransformer.java:91)
[junit] ... 20 more
[junit] Caused by: java.lang.IllegalStateException: Class org/apache/tools/ant/util/DOMElementWriter$XmlNamespacePolicy is already instrumented.
[junit] at org.jacoco.agent.rt.internal_c13123e.core.internal.instr.InstrSupport.assertNotInstrumented(InstrSupport.java:176)
[junit] at org.jacoco.agent.rt.internal_c13123e.core.internal.instr.ClassInstrumenter.visitField(ClassInstrumenter.java:55)
[junit] at org.jacoco.agent.rt.internal_c13123e.asm.ClassVisitor.visitField(ClassVisitor.java:294)
[junit] at org.jacoco.agent.rt.internal_c13123e.asm.ClassReader.readField(ClassReader.java:883)
[junit] at org.jacoco.agent.rt.internal_c13123e.asm.ClassReader.accept(ClassReader.java:694)
[junit] at org.jacoco.agent.rt.internal_c13123e.asm.ClassReader.accept(ClassReader.java:500)
[junit] at org.jacoco.agent.rt.internal_c13123e.core.instr.Instrumenter.instrument(Instrumenter.java:101)
[junit] at org.jacoco.agent.rt.internal_c13123e.core.instr.Instrumenter.instrument(Instrumenter.java:121)
[junit] ... 21 more
[junit] Test it.********.dao.ScheduleActivityDAOTest FAILED
[junitreport] Processing C:\Users\lubrano\Development\gitrepos\ECWebAppLocalRepo\ecwebapp\target\test-report\TESTS-TestSuites.xml to C:\Users\lubrano\Development\gitrepos\ECWebAppLocalRepo\ecwebapp\target\test-report\junit-noframes.html
[junitreport] Loading stylesheet C:\Users\lubrano\Development\gitrepos\ECWebAppLocalRepo\ecwebapp\src\test\resources\junit-noframes.xsl
[junitreport] Transform time: 241ms
coverage:
[jacoco:report] Loading execution data file C:\Users\lubrano\Development\gitrepos\ECWebAppLocalRepo\ecwebapp\target\coverage-report\jacoco.exec
[jacoco:report] Writing bundle 'Code Annotation Project' with 298 classes
BUILD SUCCESSFUL
Total time: 1 minute 6 seconds
我不知道为什么在做覆盖率报告时一半的测试中出错,而同时我得到的测试用例报告没有错误;在测试阶段和覆盖范围之间似乎出了点问题,但我无法说明。
感谢您的关注!