java 如何使用 Ant 在类别/套件中运行所有 JUnit 测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6226026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run all JUnit tests in a category/suite with Ant?
提问by Jonik
I'm using JUnit Categories and ClassPathSuite in a setup similar to that described in this answer. To recap:
我在类似于此答案中描述的设置中使用 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 {
}
...where AllTests makes use of the ClasspathSuitelibrary.
...其中 AllTests 使用ClasspathSuite库。
A test class that's part of the FastTests category would look like this:
属于 FastTests 类别的测试类如下所示:
@Category(FastTests.class)
public class StringUtilsTest {
// ...
}
When I run "FastTestSuite" in my IDE, all tests with the FastTests annotation are executed, nice & smooth:
当我在 IDE 中运行“FastTestSuite”时,所有带有 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.
现在,我想对 Ant 做同样的事情。(令我惊讶的是,我无法在 SO 上轻松找到相关说明。)换句话说,我需要一个 Ant 目标来运行所有带有 FastTests 注释的测试。
I've tried some simplistic approaches using <test>
or <batchtest>
...
我尝试了一些简单的方法使用<test>
或<batchtest>
...
<junit showoutput="true" printsummary="yes">
<test name="fi.foobar.FastTestSuite"/>
<formatter type="xml"/>
<classpath refid="test.classpath"/>
</junit>
... but no luck, so far.
......但到目前为止还没有运气。
Edit: Besides the IDE, it works fine with JUnitCore on the command line:
编辑:除了 IDE,它在命令行上与 JUnitCore 一起工作正常:
$ java -classpath "classes:WebContent/WEB-INF/lib/*" org.junit.runner.JUnitCore fi.foobar.FastTestSuite
.............
Time: 0.189
OK (13 tests)
采纳答案by Jonik
Right, I got it working with <batchtest>
quite simply:
是的,我<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>
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 :-)
我之前试过<batchtest>
,但犯了一个愚蠢的错误"**/FastTestSuite.java"
,"**/FastTestSuite.class"
在<include>
元素中使用而不是......对此感到抱歉:-)
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"
.
注意:有必要设置fork="yes"
(即,在单独的 VM 中运行测试);否则,这也会在 java.lang.reflect.Constructor.newInstance(Constructor.java:513) 处产生“initializationError”,例如 <test>
(请参阅对问题的评论)。但是,<test>
即使使用fork="yes"
.
The only shortcomingis 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.
该唯一的缺憾是,这将产生只有一个JUnit的XML报告文件(TEST-fi.foobar.FastTestSuite.xml),这使得它看起来像所有的(几百个)的测试是在一个类(FastTestSuite)。如果有人知道如何调整它以显示其原始类和包中的测试,请告诉我。
回答by R. Oosterholt
I found a workaround to use ant's junittask to run test of a specific Category
:
我找到了一种使用 ant 的junit任务来运行特定测试的解决方法Category
:
<target name="test" description="Run Selenium tests by category">
<fail unless="category.name" message="Please specify 'category.name' property"/>
<junit showoutput="true" printsummary="true" fork="true">
<formatter type="xml"/>
<classpath refid="test.classpath"/>
<batchtest todir="${test.reports}" haltonfailure="false">
<fileset dir="${classes}">
<!-- regex '^\s*@Category' part added to ensure @Category annotation is not commented out -->
<containsregexp expression="^\s*@Category.*${category.name}"/>
</fileset>
</batchtest>
</junit>
</target>
Execute test by supplying category.name
property to ant like this:
通过向category.name
ant提供属性来执行测试,如下所示:
ant -Dcategory.name=FastTests test
Using batchtest
will also produce separate JUnit XML report files per test (e.g. TEST-fi.foobar.FastTestClassN.xml).
使用batchtest
还将为每个测试生成单独的 JUnit XML 报告文件(例如 TEST-fi.foobar.FastTestClassN.xml)。