JanakiL
JanakiL

Reputation: 33

Re-run failed Test Script 'completely' using JUnit

I found some solution to rerun failed @Test in this forum at How to Re-run failed JUnit tests immediately?. In my case i execute the test from command line. And i want to rerun the complete test if it fails. Given below is my Test Script template and i want to rerun everything (from start to end) if it fails

@BeforeClass
public static void Start(){
...
}

@Test
public void Test_One(){
...
}

@Test
public void Test_Two(){
...
}

@AfterClass
public static void End(){
...
}

I will get to know in End() method if my test script has failed. If it fails, i would like to run everything like

@BeforeClass
@Test (all @Test)
@AfterClass

Is it possible with JUnit? I am not sure if the template that i am using is correct :(

Upvotes: 3

Views: 2522

Answers (3)

user1459144
user1459144

Reputation: 2967

More flexible solution is to write custom runner. I described this answer in the following post:

How to Re-run failed JUnit tests immediately?

Upvotes: 0

user1459144
user1459144

Reputation: 2967

JanakiL, It is a very good question. I tried to find some solution but i didn't manage to find clean solution for this task. I can only propose to do some workaround that eventually will work. So, in order to re-run suite you need to do following steps:

  1. You need to create @ClassRule in order to execute whole suite. All Suite you can retry using following code:

    public class Retrier implements TestRule{

    private int retryCount;
    private int failedAttempt = 0;
        @Override
    public Statement apply(final Statement base,
            final Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {               
    
                base.evaluate();                 
                while (retryNeeded()){
                    log.error( description.getDisplayName() + " failed");
                    failedAttempt++;                    
    
                }
    
            }
    

    }

retryNeeded() – method that determines whether you need to do retry or not

This will retry all tests in your suite. Very important thing that retry will go after @AfterClass method.

In case you need to have “green build” after successful retry you need to write a bunch of another gloomy code.

  1. You need to create @Rule that will not allow “publish” failed result. As example:

public class FailedRule extends TestWatcher {

@Override
public Statement apply(final Statement base, final Description description) {
         return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                List<Throwable> errors = new ArrayList<Throwable>();
                try {

                    base.evaluate();

                } catch (AssumptionViolatedException e) {
                    log.error("", e.getMessage());

                    if (isLastRun()) {
                        throw e;
                    }

                } catch (Throwable t) {
                    log.error("", t.getMessage());                  
                    if (isLastRun()) {
                        throw t;
                    }
                }
            };

        };
}   
}

isLastRun() – methods to verify whether it is last run and fail test only in case ir is last run.

Only last retry needs to be published to mark you test failed and build “red”. 3. And finally in your test class you need do register two rules:

@Rule
public FailedRule ruleExample = new FailedRule ();
@ClassRule 
public static Retrier retrier = new Retrier (3);

In the Retrier you can pass count of attempts to retry.

I hope that somebody could suggest better solution !

Upvotes: 1

kbdjockey
kbdjockey

Reputation: 897

Using Eclipse, in the Junit view, you have a button "ReRun Tests - Failures First"

You can also see this answer: How to Re-run failed JUnit tests immediately?

Upvotes: 0

Related Questions