I have many Java flaky tests using testNG, which may fail randomly. Now I'd like to run them repeatly until failure and see the log output. Can testNG do this?
Seems there are many questions about how to retry fail tests, but no solution about how to retry tests until failure.
CodePudding user response:
Consider using Retry Analyzers of testNG.
- Create A Retry Analyzer that will "check" the result of test execution and will decide whether to re-run the test.
public class MyRetryAnalyzer implements IRetryAnalyzer {
@Override
public boolean retry(ITestResult result) {
// analyze the result and return true if you want to rerun the test
// return false other
}
}
- Apply this retry analyzer to your tests:
public class MyTest {
@Test(retryAnalyzer = MyRetryAnalyzer.class)
public void sampleTest()
{
....
}
See an example about this feature here
CodePudding user response:
did something similar to this once, I recommend you to make a while loop, which will run the test until there is a failure.
And when there is a failure, change flag state , and it will help stop the loop
boolean flag = true;
while (flag){
// Run Test
// and if test fail change flag
flag = false ;
}
Assert.assertTrue(flag);