0

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

Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26
Bastian
  • 1,089
  • 7
  • 25
  • 74

1 Answers1

0

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);
        }
    }
Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26
  • Sorry man, it is just trying again and again the test, and not start again the whole class @Norayr Sargsyan – Bastian Jul 01 '20 at 13:32
  • Testng does not support retry operation for the class level, I think this solution can be usable for you https://stackoverflow.com/questions/50241880/retry-logic-retry-whole-class-if-one-tests-fails-selenium – Norayr Sargsyan Jul 01 '20 at 14:19
  • Nope they say to remove the @test annotation, my scenario is that in the before class I enter a specific web page, and than perform 30 tests by priority, since some tests need to run before others. and if for example test 5 fail, I need to run test 1 - 5 again. and I get full report exactly which test step fail – Bastian Jul 01 '20 at 14:43