问题
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