如何使用 Ant 在类别/套件中运行所有 JUnit 测试? [英] How to run all JUnit tests in a category/suite with Ant?
问题描述
我在类似于 这个答案.回顾一下:
I'm using JUnit Categories and ClassPathSuite in a setup similar to that described in this answer. To recap:
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 库.
...where AllTests makes use of the ClasspathSuite library.
属于 FastTests 类别的测试类如下所示:
A test class that's part of the FastTests category would look like this:
@Category(FastTests.class)
public class StringUtilsTest {
// ...
}
当我在 IDE 中运行FastTestSuite"时,所有带有 FastTests 注释的测试都会被执行,很好 &平滑:
When I run "FastTestSuite" in my IDE, all tests with the FastTests annotation are executed, nice & smooth:
现在,我想用 Ant 做同样的事情.(令我惊讶的是,我无法在 SO 上轻松找到相关说明.)换句话说,我需要一个 Ant 目标来运行所有带有 FastTests 注释的测试.
Now, I want to do the same thing with Ant. (To my surprise, I couldn't easily find instructions for this on SO.) In other words, I need an Ant target that runs all tests with the FastTests annotation.
我尝试了一些使用
或
...
I've tried some simplistic approaches using <test>
or <batchtest>
...
<junit showoutput="true" printsummary="yes">
<test name="fi.foobar.FastTestSuite"/>
<formatter type="xml"/>
<classpath refid="test.classpath"/>
</junit>
...但到目前为止还没有运气.
... but no luck, so far.
编辑:除了 IDE,它还可以在命令行上与 JUnitCore 配合使用:
Edit: Besides the IDE, it works fine with JUnitCore on the command line:
$ java -classpath "classes:WebContent/WEB-INF/lib/*" org.junit.runner.JUnitCore fi.foobar.FastTestSuite
.............
Time: 0.189
OK (13 tests)
推荐答案
是的,我用
很简单:
Right, I got it working with <batchtest>
quite simply:
<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>
我之前尝试过
,但犯了一个愚蠢的错误,使用 "**/FastTestSuite.java"
而不是 "**/FastTestSuite.class"
在 <include>
元素中...很抱歉:-)
I had tried <batchtest>
earlier, but had made the silly mistake of using "**/FastTestSuite.java"
instead of "**/FastTestSuite.class"
in the <include>
element... Sorry about that :-)
注意:必须设置fork="yes"
(即,在单独的虚拟机中运行测试);否则,这也会在 java.lang.reflect.Constructor.newInstance(Constructor.java:513) 处产生initializationError",如
(请参阅对问题的评论).但是,即使使用 fork="yes"
,我也无法让
工作.
NB: it's necessary to set fork="yes"
(i.e., run the tests in a separate VM); otherwise this will also produce "initializationError" at java.lang.reflect.Constructor.newInstance(Constructor.java:513) like <test>
(see comments on the question). However, I couldn't get <test>
working even with fork="yes"
.
唯一的缺点是这只会生成一个 JUnit XML 报告文件 (TEST-fi.foobar.FastTestSuite.xml),这使得所有(数百个)测试看起来都在一个类中(快速测试套件).如果有人知道如何调整它以在他们的原始课程中显示测试 &包裹,请告诉我.
The only shortcoming is that this produces just one JUnit XML report file (TEST-fi.foobar.FastTestSuite.xml) which makes it look like all the (hundreds) of tests are in one class (FastTestSuite). If anyone knows how to tweak this to show the tests in their original classes & packages, please let me know.
这篇关于如何使用 Ant 在类别/套件中运行所有 JUnit 测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!