Running JUnit tests and forking the JVM correctly (Ant)

huangapple go评论75阅读模式
英文:

Running JUnit tests and forking the JVM correctly (Ant)

问题

我最近开始将JUnit 4升级到JUnit 5,然后升级到Ant 1.10.8。

当我运行测试时,我看到的情况是,似乎我们没有像以前的版本那样分叉JVM。结果表明,我们在测试之间重复使用JVM,这意味着我们在运行之间会遇到静态类的问题。

<junitlauncher>
    <classpath>
        ...
    </classpath>
    <testclasses>
        <fork dir="${baseDirectory}">
            ....
        </fork>
    </testclasses>
</junitlauncher>

我是否遗漏了Ant上的分叉功能?还是有没有其他方法可以像“旧的ant和junit组合”那样运行?

英文:

I've recently started the upgrade from JUnit 4 to JUnit5 and the subsequent update to Ant 1.10.8 as well.

What I'm seeing when I run the tests is that it appears we're not forking the JVM like it did in previous versions. The results are indicating that we're reusing the JVM between tests which means we're hitting issue with static classes between runs.

&lt;junitlauncher&gt;
   &lt;classpath&gt;
      ...
   &lt;/classpath&gt;
   &lt;testclasses&gt;
      &lt;fork dir=&quot;${baseDirectory}&quot;&gt;
         ....
      &lt;/fork&gt;
   &lt;/testclasses&gt;
&lt;/junitlauncher&gt;

Is there something I'm missing here with the fork functionality on Ant?
Or is there another way around this that operates like the 'old ant & junit combo'?

答案1

得分: 2

*sigh

所以对于那些感兴趣的人 - 答案在细节中:

fork嵌套元素可用于在新分叉的JVM中运行测试。
此testclasses元素的所有测试将在新分叉的JVM的一个单一实例中运行。

因此,JUnit5不会为每个测试分叉 - 它会为testclasses属性中的所有项目进行分叉。为了让每个测试类都有一个JVM来循环遍历资源 - 需要在testclasses块的外部进行一些魔法。

英文:

*sigh

So for those interested parties - the answer is in the details:

> The fork nested element can be used to run the tests in a newly forked
> JVM. All tests that are part of this testclasses element will run
> in one single instance of the newly forked JVM.

So JUnit5 doesn't fork for each test - it forks for all the items in the testclasses attribute. In order to have a resource to be looped through with a JVM for each test class - that is going to require some magic on the outside of that testclasses block.

huangapple
  • 本文由 发表于 2020年8月7日 12:00:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63294995.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定