Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mark as failed running too long JUnit tests

Tags:

I would like to stop and mark as failed too long running junit tests (executed within Maven 3 build). I am aware of three ways of doing it:

1) Using Test annotation with timeout parameter:

@Test(timeout=100)
public void testWithTimeout() {
    ...
}

2) Using Rule annotation:

@Rule
public Timeout globalTimeout = new Timeout(100);

3) Configuring maven-surefire-plugin using following options:

forkedProcessTimeoutInSeconds=1
reuseForks=false

The clue is 1) and 2) requires to change each test (it hurts when you have many thousands). 3) solution is not acceptable as within many modules first test starts the context which is used by the test - tests performance would drastically decrease.

Do you have any other ideas how to achieve that? Any tricky solution (not involving patching JUnit :))?

like image 488
Piotr Oktaba Avatar asked Sep 29 '14 19:09

Piotr Oktaba


People also ask

Can we rerun failed test cases in JUnit?

Rerun Failed Tests with JUnit 4 ExampleWe need two classes, one of them is our Rule Class's RetryRule and the other is our Test Class's RetryRuleTest. In RetryRuleTest class, I will open www.swtestacademy.com and get its title and check it with the WRONG expected title.

What does the fail () method do in JUnit?

The junit fail method is used to verify that the actual exception will throw an error or the test is failing at the time of development. The junit fail assertion will fails while throwing error of assertion which was unconditional.

Which is the correct code for failing the test if the test takes longer than 1 second for execution?

@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).

Why is my JUnit test failing?

When writing unit tests with JUnit, there will likely be situations when tests fail. One possibility is that our code does not meet its test criteria. That means one or more test cases fail due to assertions not being fulfilled.


1 Answers

You can try to define your own test runner by writing a class which extends BlockJunit4ClassRunner and fail if the test execution exceeds the defined timeout. Then annotate your test classes with @RunWith(CustomizedTestRunner.class) You still need to modify the classes but the timeout value can be specified in a single location.

like image 125
user1048931 Avatar answered Sep 20 '22 04:09

user1048931