简体   繁体   中英

How to run class if test case failed in testNG

I have an issue that I am facing, I have a class with multiple test cases and I am using test ng with java and selenium. Is there a possibility that if a test case failed, testNG will run the entire class again? not the test. since there are a priority and navigation between pages. Is there a way to run the entire class that is failed again? I just saw to rerun the test, and it is useless to me. regards

If I understood you right this is the solution which you needed

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

public class Retry implements IRetryAnalyzer {

    private int count = 0;
    private static int maxTry = 3;

    public boolean retry(ITestResult iTestResult) {
        if (!iTestResult.isSuccess()) {
            if (count < maxTry) {
                count++;
                iTestResult.setStatus(ITestResult.SUCCESS);
                return true;
            } else {
                iTestResult.setStatus(ITestResult.FAILURE);
            }
        } else {
            iTestResult.setStatus(ITestResult.FAILURE);
        }
        return false;
    }
}

Then your all tests methods should be like this:

 @Test(retryAnalyzer=Retry.class)

And you should add the BeforeSuite

  @BeforeSuite(alwaysRun = true)
    public void beforeSuite(ITestContext context) {
        for (ITestNGMethod method : context.getAllTestMethods()) {
            method.setRetryAnalyzerClass(Retry.class);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM