A flaky TestNG checkout UI suite applies IRetryAnalyzer to every test through an IAnnotationTransformer, and a listener screenshots in onTestFailure: what goes wrong?
answer
- where was the transformer registered
- which callback a retried attempt gets
- green build, rising skips
- static counter, shared budget
basics
~20 sTestNG ignores a transformer declared in @Listeners, so retries may never happen. Once they do, retried attempts become SKIP and fire onTestSkipped, so onTestFailure screenshots miss every flaky attempt and a fail-then-pass test looks green.
solid answer
~40 sFirst check registration: TestNG's documentation says a transformer declared in `@Listeners` is ignored, so the suite may have no retries at all; it belongs in `testng.xml`, `-listener` or a ServiceLoader file. Once retries run, each retried attempt is re-marked `SKIP` with `wasRetried()` true and fires `onTestSkipped`, not `onTestFailure`. A flaky test that passes on retry leaves no screenshot, and one that always fails leaves only the last attempt's. Capture on `onTestSkipped` when `wasRetried()` is true, or in `IInvokedMethodListener.afterInvocation`, which still sees `FAILURE`. The verdict hides the flake: one skip plus one pass. Finally, look for a `static` retry counter that runs dry mid-run, and an analyzer that retries assertion failures.
code
java · 21 linesimport org.testng.ITestListener;
import org.testng.ITestResult;
public class EvidenceListener implements ITestListener {
@Override
public void onTestFailure(ITestResult result) {
capture(result, "final-failure");
}
@Override
public void onTestSkipped(ITestResult result) {
if (result.wasRetried()) {
capture(result, "retried-attempt");
}
}
private void capture(ITestResult result, String label) {
// take the screenshot here and attach it to the result
result.setAttribute("evidence", label + "-" + result.getMethod().getMethodName());
}
}go deeper
Know that a retried attempt is not reported as a failure, so a listener that acts only on failures will miss it.
Explain the registration rule for transformers and the callback a retried attempt receives, and how wasRetried() separates it from an ordinary skip.
Walk the diagnosis in order: prove retries happen, find where evidence is lost, show how the verdict hides the flake, and check the analyzer's counter and the failures it retries.
Turn the findings into a rule: retried attempts always leave evidence and are reported as their own outcome, and blanket retry is not allowed to make a red suite green silently.
## The scenario A team owns a flaky checkout UI suite on TestNG. They add two things: - an `IAnnotationTransformer` that calls `setRetryAnalyzer(...)` on every `@Test`, so every test gets up to two retries; - an `ITestListener` whose `onTestFailure(ITestResult)` takes a browser screenshot. A week later the build is green, nobody can explain the checkout flakes customers report, and the failure screenshots folder is nearly empty. Each symptom has a specific TestNG cause. ## Defect 1: the transformer may never have run If the transformer was declared in `@Listeners`, TestNG ignores it: TestNG needs the transformer before it parses annotations, `@Listeners` is one of those annotations, and the documentation warns that a transformer declared there is ignored. The suite then has **no retries at all**, and the team is debugging a policy that does not exist. How to check: 1. Look for `@Listeners` naming the transformer. 2. Confirm retries happened at all: does TestNG's console summary show a `Retries:` count, and does any result have `wasRetried()` set? 3. Move it to `<listeners>` in `testng.xml`, the `-listener` option, or a ServiceLoader file. Also check that no second transformer is registered: TestNG keeps one, and a later one replaces it with the warning `AnnotationTransformer already set`. ## Defect 2: the screenshot listener sees only the last failure Once retries work, the evidence disappears for a precise reason. When the analyzer returns `true`, TestNG re-marks that attempt as `SKIP` with `wasRetried()` set, and then calls **`onTestSkipped`, not `onTestFailure`**. So: - a test that fails, then passes on retry, produces **no screenshot at all**; - a test that fails every attempt produces one screenshot, of the **last** attempt only. The flaky moments, which are the ones you need, are exactly the ones not captured. Two fixes: - also capture in `onTestSkipped` when `result.wasRetried()` is true; - or capture in `IInvokedMethodListener.afterInvocation`, which runs **before** the retry decision and still sees status `FAILURE`; filter with `method.isTestMethod()` so configuration methods are skipped. The `ITestListener` callbacks fire before `@AfterMethod` hooks run, so if the browser is closed in `@AfterMethod` it is still open when either listener captures. ## Defect 3: the verdict hides the flake A fail-then-pass test is recorded as **one skip plus one pass**. A report that counts failures shows nothing; a report that counts skips shows a rise that looks like disabled tests. Only `wasRetried()` distinguishes a retried attempt from a skip caused by a failed `@BeforeMethod` or a failed dependency. Unless someone surfaces it, the flake rate is invisible and the checkout defect customers hit ships. ## Defect 4: the analyzer's own state - A counter in a **`static` field** is shared by every test, so after the first few failures nothing else is retried. TestNG already keeps one analyzer instance per test method, and one per parameter set for data-driven tests, so an instance field is the right place. - An analyzer that ignores `result.getThrowable()` retries **assertion failures**, re-running real product bugs until one attempt happens to pass. - A blanket transformer that sets the analyzer unconditionally overwrites any analyzer a test declared itself. ## Confirming each defect quickly 1. Search the run log for `AnnotationTransformer already set` and check where the transformer is declared. 2. Run one test that is known to be flaky, several times, and read TestNG's console summary for a `Retries:` count. 3. Add a temporary `ITestListener` that prints the method name, the status and `wasRetried()` from `onTestSkipped`; retried attempts will show up there and nowhere in `onTestFailure`. 4. Read the analyzer for `static` fields and for any check on `result.getThrowable()`. Each check takes minutes and turns a vague "retries made it worse" into a named cause. ## What a good answer concludes | Symptom | Cause | Fix | |---|---|---| | no retries happen | transformer in `@Listeners` | register in `testng.xml`, `-listener` or ServiceLoader | | no screenshot on a flaky pass | retried attempts go to `onTestSkipped` | capture on `wasRetried()` or in `afterInvocation` | | green build, rising skip count | retried attempts recorded as `SKIP` | report `wasRetried()` attempts as their own outcome | | retries stop mid-run | `static` counter | instance field, per method | A strong candidate also says that a retry should treat the flake as a signal: every retried attempt should leave evidence and a count that someone reads.
- Why does an IInvokedMethodListener.afterInvocation hook see a retried attempt as a failure when ITestListener sees it as a skip?TestNG runs `afterInvocation` before it consults the retry analyzer, so the attempt still has status `FAILURE` there. Only after `retry` returns `true` is the result re-marked `SKIP`, and then `ITestListener.onTestSkipped` fires.
- How would you make retried attempts visible in the results of this TestNG suite?Count results whose `wasRetried()` is true, in an `ITestListener` or an `IReporter` reading `ITestContext.getSkippedTests()`, and report them as their own outcome, separate from real skips. TestNG's console summary already prints a `Retries:` count when there are any, but most status-only reports fold them into skips.
It is like a smoke alarm that silences itself when the smoke clears within a minute: its log records a reset rather than an alarm, so unless someone reads the resets, the kitchen looks safe.
- A transformer works the same from @Listeners as from testng.xml
- onTestFailure fires for every failed attempt, retried or not
- A pass on retry leaves no trace in TestNG's results
- Retries only ever re-run flaky infrastructure, never real bugs
- Every skipped result means a hook or dependency failed
Assumes TestNG 7.12: a retried attempt is recorded as SKIP with wasRetried() true and reaches ITestListener through onTestSkipped.