在 Eclipse 中设置 JUnit 超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4097821/
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 15:17:17  来源:igfitidea点击:

Set JUnit timeout in eclipse

javaeclipseunit-testingjunittimeout

提问by gMale

Question

When I run all our JUnit tests, using eclipse, can I set a default timeout?

当我使用 eclipse 运行所有 JUnit 测试时,我可以设置默认超时吗?

Background

背景

My manager insists on writing Unit tests that sometimes take up to 5 minutes to complete. When I try to run our entire test suite (only about 300 tests) it can take over 30 minutes. I want to put something in place that will stop any test that takes longer than 10 seconds.

我的经理坚持编写有时需要 5 分钟才能完成的单元测试。当我尝试运行我们的整个测试套件(只有大约 300 个测试)时,它可能需要 30 多分钟。我想放置一些东西来停止任何需要超过 10 秒的测试。

I know an individual test can be annotated with:

我知道可以用以下注释对单个测试进行注释:

@Test(timeout=10000)

But doing this would make his long tests always fail. I want them to work when he runs them on his box (if I have to make minor adjustments to the project before checking it in, that's acceptable. However, deleting the timeouts from 40 different test files is not practical).

但是这样做会使他的长期测试总是失败。当他在他的盒子上运行它们时,我希望它们能够工作(如果我必须在签入之前对项目进行微调,这是可以接受的。但是,从 40 个不同的测试文件中删除超时是不切实际的)。

I also know I can create an ant task to set a default timeout for all tests, along the lines of:

我也知道我可以创建一个 ant 任务来为所有测试设置默认超时,如下所示:

<junit timeout="10000">
  ...
</junit>

The problem with that we typically run our tests from inside eclipse with Right Click > Run As > JUnit Test.

我们通常使用Right Click > Run As > JUnit Test从 Eclipse 内部运行测试的问题

Summary

概括

So is there a relatively painless way to set a timeout for all tests, perhaps using a Run Configuration setting, or project setting, or JUnit preference, or environment variable, or something? I'd even settle for installing some other plugin that lets me right click on particular test folders and run all the tests in some other manner like through ant or something...

那么是否有一种相对轻松的方法来为所有测试设置超时,也许使用运行配置设置,或项目设置,或 JUnit 首选项,或环境变量,或其他什么?我什至愿意安装一些其他插件,让我右键单击特定的测试文件夹并以其他方式运行所有测试,例如通过 ant 或其他方式......

回答by alaster

Possible solution: Extend all your Test classes from another class: TestBasefor example

可能的解决方案:从另一个类扩展所有测试类:TestBase例如

Add to TestBaseglobal timeout. This timeout will be applied to all extended classes:

添加到TestBase全局超时。此超时将应用于所有扩展类:

public class TestBase {
    @Rule
    public Timeout globalTimeout = new Timeout(10000);
}

回答by benc_cneb

So maybe a combination of using Infinitest with the "Slow test warning" enabled together with the filtering feature would do the trick. You could identify tests that exceed your time-limit and add them to the filter list, this would only affect testing from inside Eclipse. Running the tests via a possible build script via CLI/CI etc would not be affected at all. You can find more on setting this up here: http://improvingworks.com/products/infinitest/infinitest-user-guide/

因此,也许将 Infinitest 与启用的“慢速测试警告”与过滤功能结合使用就可以解决问题。您可以识别超出时间限制的测试并将它们添加到过滤器列表中,这只会影响 Eclipse 内部的测试。通过 CLI/CI 等可能的构建脚本运行测试根本不会受到影响。您可以在此处找到有关设置的更多信息:http: //improvingworks.com/products/infintest/infintest-user-guide/

回答by Nuevi

If you want to configure the tests to run for a maximum of ten seconds you can try this:

如果您想将测试配置为最多运行 10 秒,您可以尝试以下操作:

@Test(timeout=10000)

@Test(timeout=10000)

回答by Raedwald

My manager insists on writing Unit tests that sometimes take up to 5 minutes to complete

我的经理坚持编写有时需要 5 分钟才能完成的单元测试

This almost certainly indicates that those tests are not in fact unit tests. Cut that Gordian knot: try refactoring your testsuite to provide equivalent test coverage without requiring a test-case that runs for that long.

这几乎可以肯定地表明这些测试实际上并不是单元测试。解决这个难题:尝试重构您的测试套件以提供等效的测试覆盖率,而无需运行那么长时间的测试用例。

回答by Alb

Almost certainly your bosses tests are system tests pretending to be unit tests. If they are suppsoed to be unit tests and are just slow they should be refactored to use mocks so that they run quicker.

几乎可以肯定,你的老板测试是伪装成单元测试的系统测试。如果它们被假定为单元测试并且速度很慢,则应重构它们以使用模拟,以便它们运行得更快。

Anyway, a more pragmatic and diplomatic approach than confronting your boss over this might be to just try and run the faster ones yourself. I've seen a hack to do this in a project where slow tests had SytemTestin their names. Then there were two ant targets created in the build file. One that ran all tests and one that filtered out by class name the SytemTests . To implement this all you would have to do is rename some of the tests and write your ant target.

无论如何,比面对你的老板更务实和外交的方法可能是自己尝试运行更快的方法。我在一个项目中看到了一个黑客来做到这一点,其中缓慢的测试SytemTest在他们的名字中。然后在构建文件中创建了两个 ant 目标。一个运行所有测试,一个按类名过滤掉SytemTests 。要实现这一点,您所要做的就是重命名一些测试并编写您的 ant 目标。

回答by Aequitas

It sounds like test suites would help you out.

听起来测试套件会帮助你。

You can have two test suites; QuickTestsand AllTests. Include QuickTests in the AllTests suite, as well as the tests that take a long time. Then all other tests would go into the quick tests suite.

你可以有两个测试套件;QuickTestsAllTests。在 AllTests 套件中包含 QuickTests,以及需要很长时间的测试。然后所有其他测试将进入快速测试套件。

From eclipse you can run an entire test suite at once. So you would run QuickTests and that way all the other slow tests will not run.

从 Eclipse 中,您可以一次运行整个测试套件。因此,您将运行 QuickTests,这样所有其他慢速测试都不会运行。

Or see this questionon how to apply a timeout to a suite which will apply to nested suites and classes in the suite. Which can achieve similar to what you want when combined with my above suggestion.

查看有关如何将超时应用于套件的问题,该套件将应用于套件中的嵌套套件和类。结合我的上述建议,可以达到与您想要的相似的效果。

回答by brain

I know this doesnt really answer your question but the simple answer is don't!

我知道这并不能真正回答您的问题,但简单的答案是不要!

Setting timeouts conditionally is wrong beacsue then you would have Unit tests on your machine that you are always going to fail. The point of Unit tests is to be able to quickly see that you havnt broken anything. Having to check through the failed test list to make sure it's just the long running tests is ust going to make some bugs pass through the cracks.

有条件地设置超时是错误的,因为那样你会在你的机器上进行单元测试,你总是会失败。单元测试的重点是能够快速看到您没有破坏任何东西。必须检查失败的测试列表以确保它只是长时间运行的测试只会让一些错误通过裂缝。

As some of the commenters mentioned you should split out the tests into unit tests that run quickly and the slower runnning integration tests i.e. have a source folder called src/main/java for your code, src/test/java for unit tests and src/integration-test/java for the longer running tests.

正如一些评论者所提到的,您应该将测试拆分为快速运行的单元测试和运行速度较慢的集成测试,即有一个名为 src/main/java 的源文件夹用于您的代码,src/test/java 用于单元测试和 src/集成测试/java 用于更长时间运行的测试。