Rerun failed Test cases instantly Using Junit..!!

We guys encounter sometime in need to rerun our failed test cases instantly, But if we wanna retry our test case twice/thrice etc then it is not achievable with Junit. But if we are following TestNG we can do that very fast.

But in Junit as well, we can achieve this using following way and can rerun failed test cases.

We can run this by defining a TestRule, This will give you the flexibility you need. A TestRule allows you to insert logic around the test, so you would implement the retry loop. For this you need to define a class in your framework :-

public class RetryTest {
public static class Retry implements TestRule {
private int retryCount;

public Retry(int retryCount) {
this.retryCount = retryCount;
}

public Statement apply(Statement base, Description description) {
return statement(base, description);
}

private Statement statement(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Throwable caughtThrowable = null;

// implement retry logic here
for (int i = 0; i < retryCount; i++) {
try {
base.evaluate();
return;
} catch (Throwable t) {
caughtThrowable = t;
System.err.println(description.getDisplayName() + “: run ” + (i+1) + ” failed”);
}
}
System.err.println(description.getDisplayName() + “: giving up after ” + retryCount + ” failures”);
throw caughtThrowable;
}
};
}
}
}

================================================================
Now its very simple to rerun failed test cases with adding the rule before @Test:-

Say we want to run 3 retry for our test case

@Rule public Retry retry = new Retry(3);

Dwarika Dhish Mishra

My name is Dwarika Dhish Mishra, its just my name and I am trying to bring the worth of my name in to actions and wants to be the solution not the problem. I believe in spreading knowledge and happiness. More over I am fun loving person and like travelling a lot. By nature I am a tester and a solution maker. I believe in the tag line of http://ted.org “Idea worth spreading” . For the same, I have created this blog to bring more and more learning to tester fraternity through day to day learning in professional and personal life. All contents are the part of my learning and so are available for all..So please spread the contents as much as you can at your end so that it could reach to every needful people in testing fraternity. I am pretty happy that more and more people are showing interest to become the part your Abode QA blog and I think this is good sign for us all because more and more content would be before you to read and to cherish. You may write or call me at my Email id: dwarika1987@gmail.com Cell: 9999978609

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.