How does TestNG's IRetryAnalyzer decide whether a failed test runs again, and how do the retried attempts show up in the results?
answer
- asked only after a failure
- one boolean method
- earlier attempts change status
- wasRetried on ITestResult
basics
~10 sAfter a test attempt fails, TestNG calls its IRetryAnalyzer's retry(ITestResult); true re-runs the method. Each retried attempt is recorded as SKIP with wasRetried() true, so a fail-then-pass test shows one skip and one pass.
solid answer
~40 sYou attach an analyzer with `@Test(retryAnalyzer = MyRetry.class)` or through an `IAnnotationTransformer`. When an attempt ends with status `FAILURE`, TestNG calls `retry(ITestResult)`; passing and skipped attempts are never offered to it. If it returns `true`, that attempt is re-marked `SKIP` with `wasRetried()` set, `ITestListener.onTestSkipped` fires instead of `onTestFailure`, and the method runs again on the same instance, with `@BeforeMethod` and `@AfterMethod` around each attempt. When it returns `false`, the attempt stays a failure. TestNG keeps one analyzer instance per test method, or per parameter set for data-driven tests, so an instance-field counter works. `RetryAnalyzerCount` is a helper whose counter defaults to one retry.
code
java · 17 linesimport org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class TimeoutRetry implements IRetryAnalyzer {
private static final int MAX_RETRIES = 2;
private int retries = 0; // one analyzer instance per test method
@Override
public boolean retry(ITestResult result) {
boolean infraFailure = result.getThrowable() instanceof java.util.concurrent.TimeoutException;
if (infraFailure && retries < MAX_RETRIES) {
retries++;
return true;
}
return false;
}
}go deeper
Recall that retryAnalyzer on @Test points to an IRetryAnalyzer class whose retry method returns true to run the test again.
Explain the sequence: retry is consulted only on FAILURE, the retried attempt becomes SKIP with wasRetried, hooks run per attempt, and counters live in an instance per method or per data row.
Expect to diagnose a skip count that jumped after retries were enabled, a static counter that stopped retrying halfway through the suite, and listeners mistaking retried attempts for real skips.
Be ready to argue which failures deserve a retry at all, by inspecting the throwable, and how retried attempts are made visible rather than folded into skips.
## What IRetryAnalyzer is `org.testng.IRetryAnalyzer` is a one-method interface: `boolean retry(ITestResult result)`. You attach an implementation to a test with `@Test(retryAnalyzer = MyRetry.class)`, or to many tests at once through an `IAnnotationTransformer` that calls `setRetryAnalyzer`. Without one, a `@Test` carries an internal disabled analyzer and is never retried. JUnit Jupiter's own API has no retry annotation, so this is a place where TestNG offers a built-in hook. ## The decision, step by step For a test with an analyzer, TestNG runs each attempt like this: 1. **`@BeforeMethod` hooks run**, then the test method is invoked. 2. The attempt ends and its status is settled, including expected-exception handling. 3. `IInvokedMethodListener.afterInvocation` runs; at this point a failing attempt still has status `FAILURE`. 4. **If the status is `FAILURE`**, TestNG calls `retry(result)`. Passing and skipped attempts are never offered to the analyzer. 5. If `retry` returns `true`, the attempt's result is **re-marked `SKIP`** and `setWasRetried(true)` is set on it; the method will run again on the same test-class instance. 6. If it returns `false`, the result stays `FAILURE`. 7. `ITestListener` callbacks fire with the final status of that attempt, then **`@AfterMethod` hooks run**. So `@BeforeMethod` and `@AfterMethod` run around **every** attempt, while `@BeforeClass` does not run again. ## How attempts appear in the results | Attempt outcome | Recorded status | `wasRetried()` | `ITestListener` callback | |---|---|---|---| | failed, analyzer said retry | `SKIP` | `true` | `onTestSkipped` | | failed, analyzer said stop | `FAILURE` | `false` | `onTestFailure` | | passed | `SUCCESS` | `false` | `onTestSuccess` | A test that fails once and then passes therefore leaves **one skipped result and one passed result**. The retried results live in `ITestContext.getSkippedTests()` beside real skips; TestNG's console summary separates them with a `Retries:` count when there are any, but any report that only counts statuses sees them as skips. A listener must call `result.wasRetried()` to tell a retried attempt from a skip caused by a failed hook or dependency. ## Analyzer state and counting - TestNG creates **one analyzer instance per test method** and reuses it across that method's attempts, so a counter kept in an **instance field** works. - For a data-driven method it keeps **one instance per parameter set**, so each data row gets its own budget. - A `static` counter is shared by every test using the class, which exhausts the budget for everyone after the first few failures. - `org.testng.util.RetryAnalyzerCount` is an abstract helper: it holds a counter that **defaults to 1** (one retry), `setCount(int)` changes it, and you implement `retryMethod(ITestResult)` to decide whether a given failure deserves a retry. Each call decrements the counter, so a `retryMethod` that answers `false` still consumes one. - An analyzer that allows `n` retries means up to `n + 1` runs of the method. ## Retrying selectively `retry` receives the `ITestResult`, so it can inspect `result.getThrowable()` and retry only infrastructure symptoms, for example a timeout from the browser, while letting an `AssertionError` fail at once. A blanket "always retry three times" analyzer retries real product defects too. For data-provider tests, `@DataProvider(cacheDataForTestRetries = true)` is the default: a retry reuses the row the provider returned the first time. Set it to `false` to fetch fresh data for the retry. ## Interaction with listeners - The retry decision reads the status **after** `IInvokedMethodListener.afterInvocation` has run. A listener that changes the result's status there changes the decision: marking a failure as `SUCCESS` prevents the retry. - `ITestListener` receives each attempt separately, so a listener that counts results sees two results for a test that failed once and then passed. - Evidence taken only in `onTestFailure` misses every retried attempt, because those arrive through `onTestSkipped`. ## Common misconceptions - That a retried attempt is recorded as a failure. - That `@BeforeMethod` runs only before the first attempt. - That `RetryAnalyzerCount` retries three times by default.
- What does org.testng.util.RetryAnalyzerCount give you, and what is its default?It is an abstract `IRetryAnalyzer` holding a counter that defaults to 1, so one retry unless you call `setCount(int)`. You implement `retryMethod(ITestResult)` to decide whether a failure qualifies. Every call to `retry` decrements the counter, so a failure that `retryMethod` declines still uses up one.
- Does a TestNG retry re-run @BeforeClass and @BeforeMethod?Each attempt is a fresh invocation of the method, so `@BeforeMethod` and `@AfterMethod` run around every attempt. `@BeforeClass` does not run again, because the retry happens on the same test-class instance within the same class run.
- How does a TestNG listener tell a retried attempt from a test skipped because its hook failed?Both arrive through `onTestSkipped` with status `SKIP`. Call `result.wasRetried()`: it is `true` only for an attempt the retry analyzer chose to re-run.
- Retried attempts are recorded as failures in the results
- TestNG asks the analyzer after every attempt, passes included
- A static counter in the analyzer is fine because there is one per test
- @BeforeMethod runs only before the first attempt
- RetryAnalyzerCount retries three times unless configured
Assumes TestNG 7.12: retried attempts carry status SKIP with wasRetried() true, and analyzers are kept per test method and per parameter set.