junit:forkMode=“once"的影响;关于测试正确性

我想减少构建(使用 ant)运行测试所花费的时间.目前我正在使用默认的 forkMode,它 fork 一个新的每个测试类上的 vm (perTest).

I'd like to reduce the time which our build (using ant) takes for running the tests. Currently I am using the default forkMode, which forks a new vm on each test class (perTest).

我正在考虑切换到 forkMode="once" 但我不确定这是否会以某种方式结合测试,并且可能会给我误报和/或误报运行我的测试后出现阴性结果.

I am thinking about to switch to forkMode="once" but I am unsure if this will couple the tests somehow and maybe give me false positive and/or false negatives results after running my tests.

问题:

  1. 每个测试用例是否会获得一个新的 ClassLoader,从而使之前运行的所有静态引用都不再可访问/可见?

  1. Will each test case get a new ClassLoader so that all static references from previous runs are not accessible/visible anymore?

是否有其他因素导致测试依赖/测试方法的耦合可能会改变行为(除了我不使用的本机库加载)

Are there other things which lead to test dependency/coupling of test methods which may change the behavior (beside native library loading which I am not using)


更新

根据当前的答案,当使用 forkMode 时,junit 似乎总是在每个 vm/fork 的所有测试用例之间共享一个类加载器.(所以 forkMode="once" 确实意味着所有测试都有一个类加载器)

According to the current answers it seems that junit is always sharing a single classloader between all test cases per vm/fork when using forkMode. (so forkMode="once" indeed means there's one classloader for all tests)

这有很多优点(更快的测试,并且可能由于静态耦合而导致测试失败)但也有一些缺点(静态耦合只有在使用共享类加载器时才有效 -> 误报)

This has many advantages (faster tests and may cause tests to fail because of static coupling) but also some disadvantages (static coupling which will only work if a shared classloader is used -> false positive)

推荐答案

  1. 测试运行程序将有效地为您的所有测试创建一个套件并运行它们 - 因此只涉及一个类加载器.
  2. 是的,这意味着静态数据将在测试之间共享,这有时会很方便,但会迫使您减少子句之间的静态耦合,这是一件好事.
  3. 通常没有任何显式 GC,但您可以自己做.

通常在一个 VM 中运行所有测试是一件好事.它迫使您查看静态耦合并且速度更快.至关重要的是,这也是您的 IDE 运行它们的方式,而这确实是运行测试的方式 - 尽可能接近编译时的频率.

Generally running all your tests in one VM is a good thing. It forces you to look at static coupling and is a lot quicker. Crucially, it's also the way that your IDE will be running them, and that really is the way that tests should be run - as close as possible to as often as you compile.

相关文章