Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart failed test case automatically in TestNG/Selenium

I am using Selenium webdriver, in Java with TestNG to run an X amount of test cases.

What I would like, is for any test case to automatically restart (either from starting or from point of failure), as soon as it fails.

I know TestNG framework has the following method

@Override
  public void onTestFailure(ITestResult tr) {
    log("F");
  }

but I do not know how to find out which testcase it was and then how would I restart it.

like image 868
MostWanted Avatar asked Jan 03 '12 14:01

MostWanted


People also ask

How do you rerun failed test cases in TestNG automatically?

To rerun failed test runs automatically during the test run itself, we implement IRetryAnalyzer interface provided by TestNG. By overriding retry() method of the interface in your class, you can control the number of attempts to rerun a failed test case.

How do you run a test 3 times if the test fails?

If you want to rerun the test exactly after the failure you need to call the method that failed. You can get that method name from ITestResult object. If you want to rerun all the failed test cases together, then you can give the testng-failed. xml as input xml after the first execution.

Which interface is used to retry the failed test cases in TestNG?

In this article, we will learn how to retry failed test in TestNG with IRetryAnalyzer and also how to rerun all tests from scratch in a test class. To retry a failed test, we will use the IRetryAnalyzer interface. It reruns the Selenium TestNG tests when they are failed.


2 Answers

I wanted to see an example with actual code in it and found it here: Restarting Test immediately with TestNg

Observe how the below tests will each be re-run once as soon as the failure happens.

import org.testng.Assert;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
import org.testng.annotations.Test;

public class Retry implements IRetryAnalyzer {
    private int retryCount = 0;
    private int maxRetryCount = 1;

    public boolean retry(ITestResult result) {

        if (retryCount < maxRetryCount) {
            retryCount++;
            return true;
        }
        return false;
    }

    @Test(retryAnalyzer = Retry.class)
    public void testGenX() {
        Assert.assertEquals("james", "JamesFail"); // ListenerTest fails
    }

    @Test(retryAnalyzer = Retry.class)
    public void testGenY() {
        Assert.assertEquals("hello", "World"); // ListenerTest fails

    }
}
like image 66
HRVHackers Avatar answered Oct 12 '22 17:10

HRVHackers


From testng.org

Every time tests fail in a suite, TestNG creates a file called testng-failed.xml in the output directory. This XML file contains the necessary information to rerun only these methods that failed, allowing you to quickly reproduce the failures without having to run the entirety of your tests.

If you want to rerun the test exactly after the failure you need to call the method that failed. You can get that method name from ITestResult object.

If you want to rerun all the failed test cases together, then you can give the testng-failed.xml as input xml after the first execution.

like image 31
A.J Avatar answered Oct 12 '22 17:10

A.J