In any case, to re-run the test class when it does not work

I want to re-run the test class, including its @BeforeMethod , if any of them @Test fails. I have already followed the TestNG repetition logic to restart failed test cases, but I want to run the whole class.

+5
source share
2 answers

It can be done. To do this, you need to register the 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(); } 

Attention
You must have good reason to repeat the test. It is advisable to repeat the test if you know for sure that the second iteration will lead to success. If this is not the case, then you will enter an endless loop in which unsuccessful tests will continue to run and fail.

In addition, if you want to run the test case only n times, regardless of the test result, you will need to build the logic for the counter in the onTestFailure method.

----------------------------- UPDATE -------------- ------ ----------------

More Elegant Solution Discovered Implement the IRetryAnalyzer interface. This interface was provided by TestNG specifically for retrying a failed test. He provided the number of retries.

 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 will need the following annotations

  @Test(retryAnalyzer=Retry.class) 

However, in order not to add this attribute to all testing methods, take the following approach, which refers to this link. ' TestNG retryAnalyzer only works when it is defined in the @Test methods, does not work in the @Test class

  @BeforeSuite(alwaysRun = true) public void beforeSuite(ITestContext context) { for (ITestNGMethod method : context.getAllTestMethods()) { method.setRetryAnalyzer(new RetryAnalyzerImpl()); } } 

This will hopefully provide you with a testng-results.xml report.

+13
source

Please see: http://testng.org/doc/documentation-main.html#rerunning all failed tests are included in a separate xml set that you can restart.

 <suite name="allSuites"> <suite-files> <suite-file path="yourSuite.xml" /> <suite-file path="testng-failed.xml" /> ... </suite-files> </suite> 
+2
source

Source: https://habr.com/ru/post/1202310/


All Articles