Raghvendra Singh
Raghvendra Singh

Reputation: 1805

JUnit, Timeout for parameterized tests

I have used the parameterized test capability of JUnit to run the same test with different configurations again and again. The question I have is whether the time out for the test applies to each test run individually or is it collective.

To be more specific: if I have the test running with only one parameter and the timeout of the test is 10 mins, then if I add two more entries to run the same test with, does the timeout of the test become 30 mins?

If not, then how can I configure it?

Upvotes: 1

Views: 2004

Answers (2)

Matthew Farwell
Matthew Farwell

Reputation: 61695

jackrabbit is correct when he says that the timeout applies to each execution, not to the method. This is just an expansion on his/her answer. Using the following code as an example:

@RunWith(Parameterized.class)
public class ParameterWithTimeoutTest {
    @Parameters
    public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][] {{ 5 }, { 2 }, { 1 }});
    }

    private long input;

    public ParameterWithTimeoutTest(long input) {
        this.input = input;
    }

    @Test(timeout=3000)
    public void testTimeout() throws Exception {
        Thread.sleep(input*1000);
    }
}

The testTimeout method is executed three times, with parameters 5, 2, 1 respectively. Only the first execution fails due to a timeout.

If you want to set a global timeout on the execution of the class, you can use a @ClassRule Timeout:

public class ParameterWithTimeoutTest {
    @ClassRule
    public static final TestRule globalTimeout = new Timeout(5000);

    @Parameters
    public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][] {{ 5 }, { 2 }, { 1 }});
    }

    // ...

However, this fails with an InteruptedException, not a timeout error. But at least it times out. This can be used on a Suite if necessary for a set of tests.

Upvotes: 3

jackrabbit
jackrabbit

Reputation: 5653

I just looked at the JUnit code and the timeout is applied on each execution of a parameterized test method. The runs for each parameter set are independent.

Upvotes: 5

Related Questions