如何运行在一个类别/套房,所有蚂蚁JUnit测试? [英] How to run all JUnit tests in a category/suite with Ant?

查看:260
本文介绍了如何运行在一个类别/套房,所有蚂蚁JUnit测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JUnit类别和ClassPathSuite在设置类似于<一个描述href=\"http://stackoverflow.com/questions/2176570/how-to-run-all-tests-belonging-to-a-certain-category-in-junit-4/2176791#2176791\">this答案 。总结一下:

 公共接口FastTests {
}@RunWith(Categories.class)
@ Categories.IncludeCategory(FastTests.class)
@ Suite.SuiteClasses(AllTests.class)
公共类FastTestSuite {
}@RunWith(ClasspathSuite.class)
公共类AllTests中{
}

...其中AllTests中使用了 ClasspathSuite 库。

一个测试类是这样的FastTests类是这样的一部分:

  @Category(FastTests.class)
公共类StringUtilsTest {
    // ...
}

当我运行我的IDEFastTestSuite,用FastTests注释所有测试执行,好的和放大器;流畅的:

现在,我想要做同样的事情用Ant 。 (令我惊讶,我是不会轻易找到因此,对于这个指令。)换句话说,我需要运行与FastTests注释所有测试Ant目标。

我试着使用一些简单的方法&lt;试验&GT; &LT; batchtest&GT; ...

 &LT; JUnit的showoutput =真printsummary =YES&GT;
     &lt;试验NAME =fi.foobar.FastTestSuite/&GT;
     &LT;格式化类型=XML/&GT;
     &LT;类路径REFID =test.classpath/&GT;
 &LT; / JUnit的&GT;

...但没有运气,那么远。

修改:除了IDE,它正常工作与JUnitCore在命令行上:

  $的java -classpath类:的WebContent / WEB-INF / lib目录/ *org.junit.runner.JUnitCore fi.foobar.FastTestSuite
.............
时间:0.189OK(13测试)


解决方案

好吧,我得到了它的工作&LT; batchtest&GT; 很简单:


    

 &LT; JUnit的showoutput =真printsummary =是叉=YES&GT;
    &LT;格式化类型=XML/&GT;
    &LT;类路径REFID =test.classpath/&GT;
    &LT; batchtest todir =$ {} test.reports&GT;
        &LT;文件集DIR =$ {}班&GT;
            &LT;包括姓名=** / FastTestSuite.class/&GT;
        &LT; /文件集&GT;
    &LT; / batchtest&GT;
&LT; / JUnit的&GT;

我试过&LT; batchtest&GT; 较早,但取得了使用** / FastTestSuite.java的愚蠢的错误,而不是** / FastTestSuite.class&LT;&包括GT; 元素...比较遗憾的是: - )

NB :有必要将 叉=YES (即运行在一个单独的测试VM);否则这也会产生initializationError在java.lang.reflect.Constructor.newInstance(Constructor.java:513),如&LT;试验&gt; (请参阅有关问题的评论) 。但是,我不能让&lt;试验方式&gt; 甚至叉工作=YES

唯一的缺憾的是,这会产生只有一个JUnit的XML报告文件(TEST-fi.foobar.FastTestSuite.xml),这使得它看起来像所有的(几百个)的测试是在一个类(FastTestSuite)。如果有人知道如何调整这表明在原来的班级和放大器测试;包,请让我知道。

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 {
}

...where AllTests makes use of the ClasspathSuite library.

A test class that's part of the FastTests category would look like this:

@Category(FastTests.class)
public class StringUtilsTest {
    //  ...
}

When I run "FastTestSuite" in my IDE, all tests with the FastTests annotation are executed, nice & smooth:

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.

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>

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 :-)

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".

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.

这篇关于如何运行在一个类别/套房,所有蚂蚁JUnit测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆