Hom*_*001 14
我正在使用Ant的纯JUnit4测试.
这是我的构建文件的有趣部分:
<junit printsummary="yes" haltonfailure="yes">
<formatter type="xml"/>
<classpath refid="path.test"/>
<batchtest fork="yes" todir="${dir.report.unittests.xml}">
<fileset dir="src">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
Run Code Online (Sandbox Code Playgroud)
确保在Ant的lib目录中有最新版本的junit.jar文件.据我所知,所需版本随ant 1.7或更高版本提供......
Ant默认提供JUnit 3版本.JUnit 3不支持测试注释.
要使用junit任务中的JUnit 4注释,请确保在junit任务的嵌套classpath元素中提供JUnit 4 jar的位置(请参阅ant FAQ中的此条目).
<junit showoutput="yes" fork="true">
<classpath>
<!-- The location of the JUnit version that you want to use -->
<pathelement location="lib/junit-4.9b1.jar"/>
</classpath>
<formatter type="plain" usefile="false" />
<batchtest>
<fileset dir="${tests.dir}"/>
</batchtest>
</junit>
Run Code Online (Sandbox Code Playgroud)
这是覆盖ANT_HOME/lib中的ant-junit.jar的首选解决方案,因为这意味着您可以将JUnit jar保留在源代码控制中,与代码一起使用,可以直接升级到更高版本.
请注意,虽然我在上面的文件集中没有指定任何包含模式,但这意味着junit任务将尝试针对该目录结构中的所有类运行JUnit,这可能导致包含许多不包含的类任何测试取决于您如何构建源文件.