Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...