I want to rerun a test class including its @BeforeMethod
when any one of it's @Test
fails. I have already implemented TestNG retry logic to rerun the failed test cases but I want to run entire class.
It is possible to do so. For this you need to register an implementation of org.testng.ITestListener in testNg.xml as a listener
<listeners>
<listener class-name="com.xyar.OnTestFailureClass" />
</listeners
OnTestFailureClass
must implement org.testng.ITestListener
.
Implement onTestFailure as follows:
public void onTestFailure(ITestResult result) {
XmlSuite suite = new XmlSuite();
suite.setName("rerunFailedTestClasses");
XmlTest test = new XmlTest(suite);
test.setName(result.getTestName());
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(result.getTestClass().getXmlClass());
test.setXmlClasses(classes) ;
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
}
CAUTION
You must have a good reason to rerun the test. Rerunning of the test must be desired when you know for sure that the second iteration will result in success.
If this is not the case, then you will enter an infinite loop, wherein a failing tests will keep on getting executed and getting failed.
In addition, if you want to run a test case only n number of times irrespective of the test result, then you will have to build a logic for counter in the onTestFailure
method.
-----------------------------UPDATE------------------------------------
Discovered a more elegant solution Implement IRetryAnalyzer interface. This interface has been provided by TestNG specifically for the purpose of retrying a failed test. It provided the number of times the retry must be done.
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class RetryAnalyzerImpl implements IRetryAnalyzer{
private int retryCount = 0;
private int maxRetryCount = 3;
public boolean retry(ITestResult result) {
if(retryCount < maxRetryCount)
{
retryCount++;
return true;
}
return false;
}
}
you need use following annotations
@Test(retryAnalyzer=Retry.class)
However, to avoid adding this attribute to your all test methods take the following approach which is referred by this link 'TestNG retryAnalyzer only works when defined in methods @Test, does not work in class' @Test'
@BeforeSuite(alwaysRun = true)
public void beforeSuite(ITestContext context) {
for (ITestNGMethod method : context.getAllTestMethods()) {
method.setRetryAnalyzer(new RetryAnalyzerImpl());
}
}
This should hopefully provide you with testng-results.xml report.
Please, take a look: http://testng.org/doc/documentation-main.html#rerunning all failed tests are included to the separate xml suite, that can be rerun.
<suite name="allSuites">
<suite-files>
<suite-file path="yourSuite.xml" />
<suite-file path="testng-failed.xml" />
...
</suite-files>
</suite>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With