我在类似于中描述的设置中使用 JUnit 类别和 ClassPathSuite这个答案 https://stackoverflow.com/questions/2176570/how-to-run-all-tests-belonging-to-a-certain-category-in-junit-4/2176791#2176791。回顾一下:
public interface FastTests {
}
@RunWith(Categories.class)
@Categories.IncludeCategory(FastTests.class)
@Suite.SuiteClasses(AllTests.class)
public class FastTestSuite {
}
@RunWith(ClasspathSuite.class)
public class AllTests {
}
...AllTests 使用类路径套件 http://www.johanneslink.net/projects/cpsuite.jsp图书馆。
属于 FastTests 类别的测试类如下所示:
@Category(FastTests.class)
public class StringUtilsTest {
// ...
}
当我在 IDE 中运行“FastTestSuite”时,所有带有 FastTests 注释的测试都会执行,非常流畅:
Now, 我想用 Ant 做同样的事情。 (令我惊讶的是,我无法轻松地在 SO 上找到这方面的说明。)换句话说,我需要一个使用 FastTests 注释运行所有测试的 Ant 目标。
我尝试过一些简单的方法<test>
or <batchtest>
...
<junit showoutput="true" printsummary="yes">
<test name="fi.foobar.FastTestSuite"/>
<formatter type="xml"/>
<classpath refid="test.classpath"/>
</junit>
...但到目前为止还没有运气。
Edit:除了 IDE 之外,它还可以在命令行上与 JUnitCore 配合使用:
$ java -classpath "classes:WebContent/WEB-INF/lib/*" org.junit.runner.JUnitCore fi.foobar.FastTestSuite
.............
Time: 0.189
OK (13 tests)