TestNG配置configfailurepolicy="continue"后重试测试仍被跳过
configfailurepolicy="continue" I get exactly what you're running into: even after setting configfailurepolicy="continue" at the suite level, your test method with a RetryAnalyzer gets skipped on retry when an exception is thrown in the @AfterMethod configuration. Let's break down your setup, the behavior you're seeing, and why this happens.
Your Setup Details
1. TestNG Suite Configuration (testng.xml)
You configured configfailurepolicy="continue" to keep execution going even if a configuration method fails:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite" parallel="classes" thread-count="1" configfailurepolicy="continue"> <test name="Front-End"> <classes> <class name="com.tests.ExcepionAfterMethodTest"> </class> </classes> </test> </suite>
2. Test Class
Your test class includes a failing test method, an @AfterMethod that throws an exception, and alwaysRun=true on all methods to ensure they execute:
import org.testng.Assert; import org.testng.annotations.*; public class ExcepionAfterMethodTest{ @BeforeMethod(alwaysRun=true) public void beforeMethod() { System.out.println("Before Method"); } @Test (alwaysRun=true, retryAnalyzer=RetryAnalyzer.class) public void method() { System.out.println("Method"); Assert.assertTrue(false); } @Test (alwaysRun=true) public void method1() { System.out.println("Method1"); } @AfterMethod(alwaysRun=true) public void afterMethod() throws Exception { System.out.println("After Method"); throw new Exception(); } }
3. Retry Analyzer Implementation
Your retry logic is set to retry failed tests exactly once:
import org.testng.IRetryAnalyzer; import org.testng.ITestResult; public class RetryAnalyzer implements IRetryAnalyzer { private int counter = 0; private int retryLimit = 1; @Override public boolean retry(ITestResult result) { if (counter < retryLimit) { counter++; return true; } return false; } }
Actual vs. Expected Output
Actual Execution Output
Notice that on retry, the method() test is skipped (no "Method" print on line 5):
Before Method Method After Method Before Method After Method Method1 After Method
Expected Execution Output
You expect the retry to fully run the test method again:
Before Method Method After Method Before Method Method After Method Method1 After Method
Why This Happens
The configfailurepolicy="continue" setting in TestNG is designed to continue executing other test methods when a configuration method fails, not to control how the retry mechanism handles the current test method.
When your @AfterMethod throws an exception after the first run of method(), TestNG marks the test instance as having a configuration failure. On retry, TestNG prioritizes running the configuration methods (@BeforeMethod and @AfterMethod) but skips the actual test method because it associates the configuration failure with the test's retry flow. The retry analyzer only triggers for failures in the test method itself, not for configuration method failures.
Workaround Options
If you need the test method to execute on retry even when @AfterMethod fails, here are a couple of practical approaches:
Adjust Exception Handling in
@AfterMethod:
You can check if the current test is being retried and suppress the exception in that scenario. For example:@AfterMethod(alwaysRun=true) public void afterMethod(ITestResult result) throws Exception { System.out.println("After Method"); // Skip throwing exception if this is a retry attempt RetryAnalyzer retryAnalyzer = (RetryAnalyzer) result.getMethod().getRetryAnalyzer(); if (!retryAnalyzer.retry(result)) { throw new Exception(); } }Note: You'll need to ensure your retry analyzer tracks state correctly for this to work.
Custom TestNG Listener:
Implement anIInvokedMethodListenerto override TestNG's default behavior and force the test method to run on retry, even if a configuration method failed previously. This gives you full control over the execution flow.
内容的提问来源于stack exchange,提问作者Sandeep Nalla

