How To Rerun Failed Test Cases In Selenium Using TestNG

To achieve this we have two approaches in TestNG:

  • i) By using the testng-failed.xml file in the test-output folder
  • ii) By implementing TestNG IRetryAnalyzer

1) TestNG:

It provides additional functionalities such as test annotations, grouping, prioritization, parameterization, and sequencing techniques in the code – features that were not provided earlier in JUNIT .

TestNG framework not only manages test cases, but also provides detailed reports of those tests. It provides a detailed summary that displays the number of test cases that have failed.

Whenever we run test suite using testng.xml, a testng-failed.xml file gets created in the test-output folder. Later we can run this xml file to rerun the failed testcases.

Rerun failed test cases by using TestNG-failed.xml

Steps:

i) Create a java class with tests.
ii)Here we created 3 java classes with 2 tests each having one failed assertion to fail the test.
iii)Create a testng.xml file: Right click on project=> TestNG=> Convert to TestNG
iv)Now run the testng.xml file: Right Click=> Run As=>TestNG Suite
v)Now the console output is as below with 3 failed tests.
vi)In Test output folder we have testng-failed.xml file created. To see the test output folder refresh the project. This file keeps track of the failed tests, so running this file, runs the failed tests only.
vii)Run this testng-failed.xml file: Right click=> Run As => TestNG Suite.

2) Rerunning failed tests by implementing TestNG IRetryAnalyzer:

Sometimes the tests fail due to different reasons other than bugs in the application. In order to make sure that the failure reported in the test report is genuine and not just one-off cases, we can retry running the failed test cases to eliminate false-negative test results in our test reports.

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.

i) Create an implemented class of IRetryAnalyzer interface and override retry method.

ii)public class RetryAnalyzer implements IRetryAnalyzer {

//Counter to keep track of retry attempts
int retryAttemptsCounter = 0;

//The max limit to retry running of failed test cases
//Set the value to the number of times we want to retry
int maxRetryLimit = 3;

//Method to attempt retries for failure tests
public boolean retry(ITestResult result) {
if (!result.isSuccess()) {
if(retryAttemptsCounter < maxRetryLimit){
retryAttemptsCounter++;
return true;
}
}
return false;
}
}

iii) Now add an attribute called “retryAnalyzer” to @Test annotated method with value as Implemented class name.

iv) Now if we run TestNG.xml we see failed test case is executed one more time as we gave retry count= 1. Test case is marked as failed only after it reaches max retry count.

v) where failed test case passed after 2 reruns,so we are changing retry count = 3 in RetryAnalyzer class.

Automation Tester Job Automation Testing Bhopal Job Openings Books Books to Buy on Amazon Business Analyst Career Development Companies Companies In Mumbai Developer Jobs DevOps Engineer Electronics Companies Freshers Jobs Software Hotels In India Insurance Interview JavaScript Job Opportunities Job Search Tips Must-Read Indian Books News Quality Assurance Careers Resume Writing Sales Jobs Scrum Master Service Software Testing Top 10 Books in India