Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set JUnit timeout in eclipse

Question

When I run all our JUnit tests, using eclipse, can I set a default timeout?

Background

My manager insists on writing Unit tests that sometimes take up to 5 minutes to complete. When I try to run our entire test suite (only about 300 tests) it can take over 30 minutes. I want to put something in place that will stop any test that takes longer than 10 seconds.

I know an individual test can be annotated with:

@Test(timeout=10000)

But doing this would make his long tests always fail. I want them to work when he runs them on his box (if I have to make minor adjustments to the project before checking it in, that's acceptable. However, deleting the timeouts from 40 different test files is not practical).

I also know I can create an ant task to set a default timeout for all tests, along the lines of:

<junit timeout="10000">
  ...
</junit>

The problem with that we typically run our tests from inside eclipse with Right Click > Run As > JUnit Test.

Summary

So is there a relatively painless way to set a timeout for all tests, perhaps using a Run Configuration setting, or project setting, or JUnit preference, or environment variable, or something? I'd even settle for installing some other plugin that lets me right click on particular test folders and run all the tests in some other manner like through ant or something...

like image 876
gMale Avatar asked Nov 04 '10 14:11

gMale


People also ask

How do I set timeout in JUnit?

JUnit provides a handy option of Timeout. If a test case takes more time than the specified number of milliseconds, then JUnit will automatically mark it as failed. The timeout parameter is used along with @Test annotation. Let us see the @Test(timeout) in action.

What does @test timeout 1000 annotation does in JUnit framework?

Annotations for Junit testing @Test annotation specifies that method is the test method. @Test(timeout=1000) annotation specifies that method will be failed if it takes longer than 1000 milliseconds (1 second).

Which annotation is used to run a test case with timeout configuration?

The @Timeout annotation allows one to declare that a test, test factory, test template, or lifecycle method should fail if its execution time exceeds a given duration. The time unit for the duration defaults to seconds but is configurable. For example : @Test @Timeout(value = 100, unit = TimeUnit.


1 Answers

Possible solution: Extend all your Test classes from another class: TestBase for example

Add to TestBase global timeout. This timeout will be applied to all extended classes:

public class TestBase {
    @Rule
    public Timeout globalTimeout = new Timeout(10000);
}
like image 144
alaster Avatar answered Oct 12 '22 06:10

alaster