Restarting a failed test automatically in TestNG / Selenium

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

I would like for any test case to automatically restart (either from the start or from the point of failure) as soon as it fails.

I know that TestNG structure has the following method

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

but I don’t know how to find out which test file it was in, and then how to restart it.

+5
source share
2 answers

I wanted to see an example with the actual code in it and found it here: Restarting the test using TestNg

Observe how each of the following tests will restart once as soon as a failure occurs.

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

    }
}
+9

testng.org

, , TestNG testng-failed.xml . XML , , , .

, , . ITestResult.

, testng-failed.xml xml .

+5

All Articles