在eclipse中设置JUnit超时

发布于 2024-09-30 18:51:47 字数 727 浏览 4 评论 0原文

问题

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

背景

我的经理坚持编写单元测试,有时需要长达 5 分钟才能完成。当我尝试运行整个测试套件(仅约 300 个测试)时,可能需要 30 多分钟。我想采取一些措施来停止任何耗时超过 10 秒的测试。

我知道单个测试可以注释为:

@Test(timeout=10000)

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

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

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

我们通常通过右键单击 > 从 Eclipse 内部运行测试的问题运行方式> JUnit 测试。

总结

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

Question

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

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.

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).

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

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

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

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

标点 2024-10-07 18:51:47

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

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

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

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

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

public class TestBase {
    @Rule
    public Timeout globalTimeout = new Timeout(10000);
}
九厘米的零° 2024-10-07 18:51:47

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

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/

清君侧 2024-10-07 18:51:47

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

@Test(timeout=10000)

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

@Test(timeout=10000)

泪是无色的血 2024-10-07 18:51:47

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

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

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

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.

維他命╮ 2024-10-07 18:51:47

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

不管怎样,比在这件事上与老板对峙更务实、更外交的方法可能是尝试自己运行更快的程序。我在一个项目中看到过这样做的黑客,其中慢速测试的名称中包含 SytemTest 。然后在构建文件中创建了两个 ant 目标。一种运行所有测试,另一种按类名过滤掉 SytemTest 。要实现这一点,您所要做的就是重命名一些测试并编写您的 ant 目标。

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 SytemTest in 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.

沧笙踏歌 2024-10-07 18:51:47

听起来测试套件可以帮助你。

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

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

或者查看此问题 关于如何对套件应用超时,该超时将应用于嵌套套件和套件中的类。结合我上面的建议,可以实现与您想要的类似的效果。

It sounds like test suites would help you out.

You can have two test suites; QuickTests and 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.

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.

Or see this question on 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.

我为君王 2024-10-07 18:51:47

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文