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

1

5

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.

Enravish answered 17/5, 2013 at 12:21 Comment(0)
L
3

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.

Lucrecialucretia answered 19/5, 2013 at 9:25 Comment(2)
Thanks, this has to do it. As I understand, there is no way to define a rule on the suit level but have it applied on the method level.Enravish
just an update: that Timeout has been depricated. Use @ClassRule public static Timeout timeout = new Timeout(5,TimeUnit.MINUTES);Soelch

© 2022 - 2024 — McMap. All rights reserved.