在eclipse中设置JUnit超时 [英] Set JUnit timeout in eclipse

查看:243
本文介绍了在eclipse中设置JUnit超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



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

背景



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



我知道一个单独的测试可以注释:

  @Test(timeout = 10000)

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



我也知道我可以创建一个蚂蚁任务来设置所有测试的默认超时,方法如下:

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

我们通常从eclipse内部运行我们的测试,问题是右键单击>运行> JUnit测试。



摘要



相对无痛的方法来设置所有测试的超时时间,也许使用运行配置设置或项目设置,或JUnit首选项,环境变量或某些东西?我甚至会安装一些其他插件,让我右键点击特定的测试文件夹,并以其他方式运行所有的测试,如通过ant或某些...

解决方案

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



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

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


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

解决方案

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);
}

这篇关于在eclipse中设置JUnit超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆