【问题标题】:How to run all JUnit tests in a category/suite with Ant?如何使用 Ant 在一个类别/套件中运行所有 JUnit 测试?
【发布时间】:2011-06-03 10:40:46
【问题描述】:

我在类似于this answer 中描述的设置中使用 JUnit 类别和 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 上找不到这方面的说明。)换句话说,我需要一个 Ant 目标,它使用 FastTests 注释运行所有测试。

我尝试了一些使用<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)

【问题讨论】:

  • is name="..." 是 org/test/TestSuite 之类的路径还是在包注释 org.test.TestSuite 中?错误是什么?
  • @oers:后者;完全限定的类名。没有真正的错误消息,它只是......失败了。 unittest-fast: [junit] Running fi.foobar.FastTestSuite [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0.053 sec [junit] Test fi.foobar.FastTestSuite FAILED
  • 将格式化程序更改为普通格式并将 printsummary 更改为 withOutAndErr 。这应该显示错误的原因。我认为那里有一个异常。您的设置绝对是执行套件的正确方式。
  • @oers,谢谢!实际上,即使没有“withOutAnderr”,错误也存在(我忘了我需要查看单独的报告文件):Testcase: initializationError took 0.002 sec Caused an ERROR null at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) Btw,正如我在问题上编辑的那样,这适用于 JUnitCore,这似乎令人鼓舞......
  • 嗨。我有一个相关的问题。随时插话:stackoverflow.com/questions/15776718/…

标签: java ant junit junit4


【解决方案1】:

好的,我很简单地使用&lt;batchtest&gt;

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

我之前尝试过&lt;batchtest&gt;,但犯了一个愚蠢的错误,即在&lt;include&gt; 元素中使用"**/FastTestSuite.java" 而不是"**/FastTestSuite.class"...对此感到抱歉:-)

NB:必须设置 fork="yes"(即在单独的 VM 中运行测试);否则,这也会在 java.lang.reflect.Constructor.newInstance(Constructor.java:513) 处产生“initializationError”,例如&lt;test&gt;(请参阅有关问题的 cmets)。但是,即使使用 fork="yes",我也无法让 &lt;test&gt; 工作。

唯一的缺点是这只会生成一个 JUnit XML 报告文件 (TEST-fi.foobar.FastTestSuite.xml),这使得所有(数百个)测试看起来都在一个类中(快速测试套件)。如果有人知道如何调整它以在其原始类和包中显示测试,请告诉我。

【讨论】:

  • 使用来自胡萝卜搜索的随机测试项目中捆绑的 junit4 Ant 任务:[labs.carrotsearch.com/randomizedtesting.html] 我能够使用类别和 ClassPathSuite 运行测试 - 但不需要在 中。他们的 JSON 类型报告生成输出报告,其中包含按原始包分解的测试 - 而且看起来也很漂亮。
  • 有没有办法通过在 build.xml 中指定类别名称(类)本身来做到这一点?就像在 Maven 中你可以说'mvn test -Dgroups=test.classpath.FastTests'
  • 解决您的缺点的方法是在 junitreport 之前应用 xslt。您只需要将初始 xml 拆分为较小的部分
  • 添加了另一个答案来修复您的“shortcomming”,因为它会生成单独的 JUnit XML 报告文件。
【解决方案2】:

我找到了一种解决方法,可以使用 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>

通过向 ant 提供 category.name 属性来执行测试,如下所示:

ant -Dcategory.name=FastTests test

使用 batchtest 还会为每个测试生成单独的 JUnit XML 报告文件(例如 TEST-fi.foobar.FastTestClassN.xml)。

【讨论】:

  • 我无法重现这一点,因为containsregexp 似乎正在类文件中搜索该类别的文本。但是,该文本不在二进制文件中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 2011-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-30
相关资源
最近更新 更多