Can I apply a time limit for all the tests in the suite

☆樱花仙子☆ 提交于 2019-11-28 08:12:01

问题


Is there a way in JUnit to define a global timeout/limit for all the tests included in the suite.

Well, let me to explain this a little.

I want to introduce a junit test suite into the build system of a legacy project that would be run on every build. The suite should consist of fast running unit tests, but the project has mixed set of integration and unit tests in it's tests source folder.

So what I am aiming for is introducing a central test suite for unit tests that only contains those tests that are capable of running within a certain time limit (each test individually).

I want to enforce that rule by introducing a @Rule annotation if possible.

I did try this:

@RunWith(Suite.class)
@SuiteClasses({...})
public class FastTestSuite{
    @Rule Timeout timeout = new Timeout(1);
}

but this did not seem to do the trick. I know for a fact that there are tests that tun much slower than 1 millisecond, but none of the tests failed.


回答1:


You can use a @ClassRule in your suite.

@RunWith(Suite.class)
@SuiteClasses({ Test1.class, Test2.class})
public class SuiteWithTimeout {
    @ClassRule
    public static Timeout timeout = new Timeout(1000);
}

Note, that the timeout will be per class (or per suite if you have nested them) not per test method.

Also have a look at this post.



来源:https://stackoverflow.com/questions/16608934/can-i-apply-a-time-limit-for-all-the-tests-in-the-suite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!