我有一个Java项目,其中包含使用JUnit编写的一些单元测试。最近添加了一些新的单元测试,这些测试是用groovy(也使用JUnit)编写的,因为它更容易制作那些 更具表现力,通常更容易阅读。它还允许我们使用spock框架。
该项目是使用 ant 构建和测试的。
在添加groovy类之前,使用以下ant任务运行单元测试:
<target name="test" depends="test-compile">
<junit printsummary="yes">
<classpath>
<path refid="test.classpath"/>
</classpath>
<formatter type="plain"/>
<batchtest fork="yes" todir="${test.dir}/report">
<fileset dir="${test.dir}/unit" includes="**/*.java"/>
</batchtest>
</junit>
</target>
然而,这种方法对于常规测试不起作用,因为那些在*.groovy
文件中, JUnit Ant task ,可以理解,在fileset
中无法识别它们}。
另一种方法是使用*.class
batchtest
的{{1}}个文件,如下所示:
fileset
这会生成 false negatives ,因为还包含了闭包类文件,因此可能的解决方法是排除这些文件。
<batchtest fork="yes" todir="${test.dir}/report">
<fileset dir="${test.dir}/${build.dir}">
<include name="**/*Test*.class" />
</fileset>
</batchtest>
有没有更好的方法来识别 junit ant 任务的测试类?也许一个基于反射和<batchtest fork="yes" todir="${test.dir}/report">
<fileset dir="${test.dir}/${build.dir}">
<include name="**/*Test*.class" />
<exclude name="**/*$*.class" />
</fileset>
</batchtest>
属性手动列出所有测试类(这将非常好地工作)并不是一个真正可维护的解决方案。类似Spock framework中的@Test
。
答案 0 :(得分:3)
答案 1 :(得分:0)
看看:
http://www.ibm.com/developerworks/java/library/j-pg11094/
有一个groovyc ant taskdef用于编译groovy测试用例并运行它们。 Maven就是一个例子,但要让它适应你想做的事情应该不会太难。
答案 2 :(得分:-1)
难道你不能写这样的东西吗?
<fileset dir="${test.dir}/unit" includes="**/*.java,**/*.groovy"/>