运行 <batchtest> 的每个测试在单独的 VM 中使用 Ant 中的 JUnit [英] Run each test of <batchtest> in separate VM using JUnit in Ant

查看:37
本文介绍了运行 <batchtest> 的每个测试在单独的 VM 中使用 Ant 中的 JUnit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Ant 构建文件,它使用 junit 的 batchtest 来测试所有测试类.

I have a Ant build file which uses batchtest of junit to test all the test classes.

假设我有一堂课

class MyTest{
    @BeforeClass
    public static void setUpBeforeClass(){
       //some stuff
    }

    @Test
    public void test1(){
    }

    @Test
    public void test2(){
    }
}

我知道 JUnit 会为每个测试方法创建一个 MyTest 类的新实例,但我想要的是应该为每个测试方法创建一个新的 VM.我希望每个测试方法都在单独的 VM 中运行,并希望类加载器为每个测试方法再次加载 MyTest.这可能吗?

I know that JUnit creates a new instance of MyTest class for each test method, but what I want is that a new VM should be created for each test method. I want each test method to rum in a separate VM and want classloader to load MyTest again for each test method. Is that possible?

我尝试阅读文档并尝试了此解决方案:

I have tried to read the documentation and tried this solution:

<junit fork="yes" reloading="true" forkmode="perTest">
    <batchtest>
    </batchtest>
</junit>

但即使在为我的每个测试方法使用这些选项后,setUpBeforeClass 方法也仅被调用一次.我做错了什么吗?

But even after using these options for each of my test method the setUpBeforeClass method is called only once. Am I doing something wrong?

我为什么要这样做?

我的测试方法正在使用一些使用 static 内容的合作者,我希望在每个方法上都清除 static 内容.实际上,我面临的问题是测试在本地环境中通过而在生产环境中失败.

My test methods are using some collaborators that use static stuff and I want that static stuff to be cleared on every method. Actually I am facing an issue in which tests pass at local environment and fail at production.

推荐答案

我能看到的唯一可行方法是拥有多个 元素实例.

The only possible way I can see that working is to have multiple instances of <test> elements.

问题在于您必须为每个 <test> 元素指定要运行的方法.我创建了一个测试类,其中有一个静态初始化程序和两个测试.通过以下 <junit> 任务,我可以做您想做的事:

The problem with that is that you have to specify for each <test> element what methods you want to run. I created a single test class that have a static initializer and two tests in it. With the following <junit> task I was able to do what you want:

<junit fork="yes">
    <classpath>
        <path refid="classpath" />
    </classpath>
    <formatter type="xml"/>
    <test name="TestSimple" methods="testOne" toDir="firstRun" />
    <test name="TestSimple" methods="testTwo" toDir="secondRun"  />
</junit>

从我从构建日志中得到的信息来看,很明显它们在两个不同的 JVM 中运行:

From what I get from the build logs, it's clear that they ran in two different JVMs:

test:
    ...
    [junit] Executing 'C:\Program Files\Java\jdk1.7.0_25\jre\bin\java.exe' with arguments:
    [junit] '-classpath'
    ...
    [junit] 'org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner'
    [junit] 'TestSimple'
    [junit] 'methods=testOne'
    ...
    [junit] Executing 'C:\Program Files\Java\jdk1.7.0_25\jre\bin\java.exe' with arguments:
    [junit] '-classpath'
    ...
    [junit] 'org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner'
    [junit] 'TestSimple'
    [junit] 'methods=testTwo'
    ...
    [junit] Test TestSimple FAILED

我还看到了我在每个测试报告中放入静态初始化程序的 System.out 语句.

I also see the System.out statement that I put in the static initializer in each test's report.

这里的重要细节是,您必须使每个 标记将它们的报告重定向到不同的目录,使用属性 toDir 或更改名称使用属性 outfile 的输出文件.那是因为报告文件名使用类的名称,而不是您正在运行的方法.这意味着第二次运行将覆盖第一次运行的报告.

The important detail here is that you have to make each <test> tag redirect their report to a different directory, using the attribute toDir or change the name of the output file using the attribute outfile. That's because the report file name uses the name of the class and not the method you're running. Which means the second run will overwrite the first run's report.

在您的特定情况下,您可以将 用于可以在同一 JVM 中运行而不会出现问题的所有测试,并添加 标记以避免运行有问题的测试.然后为每个有问题的测试添加一个特定的 标签.

In your specific case, you could use a <batchtest> to all tests that can run in the same JVM without problems and add an <exclude> tag to avoid running the problematic tests. Then add a specific <test> tag for each problematic test.

他们这样做是因为 methods 属性专门用于重新运行特定方法或单独运行太慢的测试.来自文档:

They do that because the methods attribute is specifically designed to re-run specific methods or separate tests that are running too slow. From the documentation:

methods 属性在以下场景中很有用:

The methods attribute can be useful in the following scenarios:

  • 测试方法失败,您想重新运行测试方法以在 Java 调试器下测试修复或重新运行测试,而无需等待其他(可能长时间运行)测试方法完全的.
  • 一个或多个测试方法的运行速度比预期的要慢并且您想在 Java 分析器下重新运行它们(没有在其他测试方法正在运行时运行分析器的开销执行).

但就个人而言,并且来自一个与您之前遇到过同样问题的人,静态初始化器很糟糕!我会非常努力地尽快摆脱它们.引用 @PeterNiederwieser 的话,为每组测试使用一个新的 JVM 会使测试套件的运行速度非常慢!

But personally, and coming from a person that ran into the same problem as you before, static initializers suck! I would try really hard to get rid of them as soon as possible. And to quote @PeterNiederwieser, having a new JVM for each set of tests will make running your test suite very slow!

这篇关于运行 &lt;batchtest&gt; 的每个测试在单独的 VM 中使用 Ant 中的 JUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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