Jon*_*nik 22 java ant junit junit4
我在类似于本答案中描述的设置中使用JUnit Categories和ClassPathSuite .回顾一下:
public interface FastTests {
}
@RunWith(Categories.class)
@Categories.IncludeCategory(FastTests.class)
@Suite.SuiteClasses(AllTests.class)
public class FastTestSuite {
}
@RunWith(ClasspathSuite.class) 
public class AllTests {
}
... AllTests使用ClasspathSuite库.
属于FastTests类别的测试类如下所示:
@Category(FastTests.class)
public class StringUtilsTest {
    //  ...
}
当我在IDE中运行"FastTestSuite"时,所有使用FastTests注释的测试都会执行,非常流畅:

现在,我想用Ant做同样的事情.(令我惊讶的是,我无法在SO上轻松找到相关说明.)换句话说,我需要一个使用FastTests注释运行所有测试的Ant目标.
我试过一些简单的方法使用<test>或<batchtest>...
 <junit showoutput="true" printsummary="yes">
     <test name="fi.foobar.FastTestSuite"/>
     <formatter type="xml"/>
     <classpath refid="test.classpath"/>
 </junit>
......但到目前为止没有运气.
编辑:除了IDE,它在命令行上与JUnitCore一起正常工作:
$ java -classpath "classes:WebContent/WEB-INF/lib/*" org.junit.runner.JUnitCore fi.foobar.FastTestSuite
.............
Time: 0.189
OK (13 tests)
Jon*_*nik 14
是的,我<batchtest>很简单地使用它:
<junit showoutput="true" printsummary="yes" fork="yes">
    <formatter type="xml"/>
    <classpath refid="test.classpath"/>
    <batchtest todir="${test.reports}">
        <fileset dir="${classes}">
            <include name="**/FastTestSuite.class"/>
        </fileset>
    </batchtest>
</junit>
我之前尝试<batchtest>过,但是犯了一个愚蠢的错误,"**/FastTestSuite.java"而不是"**/FastTestSuite.class"在<include>元素中使用...抱歉:-)
注意:有必要设置fork="yes"(即在单独的VM中运行测试); 否则这也会在java.lang.reflect.Constructor.newInstance(Constructor.java:513)中产生"initializationError"   <test>(请参阅问题评论).但是,我<test>甚至无法工作fork="yes".
该唯一的缺憾是,这将产生只有一个JUnit的XML报告文件(TEST-fi.foobar.FastTestSuite.xml),这使得它看起来像所有的(几百个)的测试是在一个类(FastTestSuite).如果有人知道如何调整它以显示其原始类和包中的测试,请告诉我.