在 Eclipse 的单独 JVM 中运行每个 JUnit 测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2890259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:46:33  来源:igfitidea点击:

Running each JUnit test in a separate JVM in Eclipse?

javaeclipseunit-testingjunitjunit4

提问by HenryR

I have a project with nearly 500 individual tests in around 200 test classes. Some of these tests don't do a great job of tearing down their own state after they're finished, and in Eclipse this results in some tests failing. The tests all pass when running the test suite from the command line via Ant.

我有一个项目,其中包含大约 200 个测试类中的近 500 个单独测试。其中一些测试在完成后并没有很好地销毁它们自己的状态,而在 Eclipse 中这会导致一些测试失败。通过 Ant 从命令行运行测试套件时,测试全部通过。

Can I enable 'test isolation' somehow in Eclipse? I don't mind if it takes longer to run.

我可以在 Eclipse 中以某种方式启用“测试隔离”吗?我不介意是否需要更长的时间才能运行。

Long term, I will clean up the misbehaving tests, but in the short term I'd like to get the tests working.

从长远来看,我会清理行为不端的测试,但在短期内,我想让测试正常工作。

采纳答案by Chris Dennett

If you use Ant in Eclipse, you can set the JUnit task to fork a new JVM process for each test, providing isolation.

如果在 Eclipse 中使用 Ant,则可以设置 JUnit 任务为每个测试派生一个新的 JVM 进程,提供隔离。

http://ant.apache.org/manual/Tasks/junit.html

http://ant.apache.org/manual/Tasks/junit.html

回答by Igor Maznitsa

I also had similar needs and developed small maven plugin Jutewhich has been published in maven central. It starts external JVM process for each JUnit test method.

我也有类似的需求并开发了小型 maven 插件 Jute,该插件已在 maven central 上发布。它为每个 JUnit 测试方法启动外部 JVM 进程。