forked from junit-team/junit4
-
Notifications
You must be signed in to change notification settings - Fork 0
Timeout for tests
lpellegr edited this page Nov 21, 2012
·
4 revisions
Tests that 'runaway' or take too long, can be automatically failed. There are two options for implementing this behaviour:
You can optionally specify timeout in milliseconds to cause a test method to fail if it takes longer than that number of milliseconds. If the time limit is exceeded, the the failure is triggered by a TimeoutException
being thrown:
@Test(timeout=1000)
public void testWithTimeout() {
...
}
The Timeout Rule applies the same timeout to all test methods in a class:
public class HasGlobalTimeout {
public static String log;
@Rule
public Timeout globalTimeout = new Timeout(10000); // 10 seconds max per method tested
@Test
public void testInfiniteLoop1() {
log += "ran1";
for (;;) {
}
}
@Test
public void testInfiniteLoop2() {
log += "ran2";
for (;;) {
}
}
}