skip to content

As the lead of a TestNG UI suite, what retry policy would you set for IRetryAnalyzer, and how would you stop retries from hiding flaky tests?

level: principalimportance: should knowfreq 35%

answer

  1. noise versus hidden defects
  2. opt-in or blanket
  3. inspect the throwable
  4. make wasRetried visible
  5. every retry has an owner

basics

~20 s

A sound TestNG retry policy is opt-in per @Test rather than a blanket transformer, capped at about one retry, limited to infrastructure exceptions, and makes every wasRetried() attempt visible, with evidence and an owner, so flakes get fixed.

solid answer

~40 s

There is no single right answer, but I would set a default and defend it. Retries are opt-in through `@Test(retryAnalyzer = ...)`, not a blanket `IAnnotationTransformer`, so the policy is visible in review. One shared analyzer, for example a `RetryAnalyzerCount` subclass, allows one retry and checks `result.getThrowable()`: infrastructure symptoms like a browser timeout qualify, an `AssertionError` never does. A shared listener records every attempt with `wasRetried()` as its own outcome, with evidence, because otherwise those attempts are just skips beside a green pass. Each opted-in test carries a ticket and loses the retry when it is fixed. If the team prefers zero retries and noisy builds, that is also defensible.

go deeper

for a junior

Know that retries can make a flaky test look like it passed, which is why teams set rules about them.

for a middle

Describe the TestNG levers: opt-in on @Test versus a transformer, the RetryAnalyzerCount cap, and checking the throwable inside retry.

for a senior

Show you have seen blanket retry hide defects and can name the TestNG signals, wasRetried and the skip counts, that reveal it.

for a principal

Defend a default against both extremes, explaining what the team gives up in noise or in hidden defects, and how the policy is enforced, measured and expired.

## Why this is a lead's decision `IRetryAnalyzer` makes retrying cheap: one transformer and every test in a TestNG suite retries. The cost is not runtime alone. A retry turns a failing attempt into a `SKIP` result and lets a later pass make the test green, so a team that retries everything stops seeing its flaky tests and, sometimes, its real defects. There is no single right policy. The lead has to choose one that fits the suite's flake rate and how the team treats the results, and then defend it. ## The decisions in the policy 1. **Opt-in or blanket.** Declaring `@Test(retryAnalyzer = ...)` on chosen tests keeps the policy visible in the code and in review. A blanket `IAnnotationTransformer` is quicker but invisible to anyone reading a test, and TestNG keeps only one transformer, so it also competes with any other transformer the suite needs. 2. **Which failures qualify.** `retry(ITestResult)` receives the result, so the analyzer can inspect `result.getThrowable()`. A common rule: retry known infrastructure symptoms (a browser timeout, a lost session) and never an `AssertionError`, which is the product disagreeing with the test. 3. **How many attempts.** One retry already absorbs many intermittent failures; each extra attempt lengthens a failing build. `RetryAnalyzerCount` defaults to one retry; deciding to raise it should need a reason. TestNG keeps one analyzer per test method and per data-provider row, so the cap applies per row. 4. **Fresh or cached data.** `@DataProvider(cacheDataForTestRetries = true)` is the default, reusing the same row; turning it off fetches fresh data, which can help when the row itself was consumed, and can also hide a data bug. 5. **Visibility.** Every retried attempt carries `wasRetried()`. The policy should require that retried attempts are reported as their own outcome, captured with evidence, and counted per test over time. 6. **Expiry.** A test that needs a retry is a test with a known defect. Many teams attach a ticket to every opted-in test and remove the opt-in when it is fixed or after an agreed period. ## Typical positions | Position | Gains | Costs | |---|---|---| | No retries at all | every flake is visible and gets fixed | noisy red builds while the backlog is worked down | | Opt-in, capped at one, infrastructure exceptions only | green builds for known flakes, policy visible in code | someone must curate the opt-in list | | Blanket retry via transformer | quiet builds immediately | flakes and some real bugs disappear from view | Most leads end up near the middle row, and they say why: the cost of noise versus the cost of a hidden defect depends on how often the suite is red and how much the team trusts it. ## Making it enforceable in TestNG - Ship one shared analyzer, for example a `RetryAnalyzerCount` subclass whose `retryMethod` checks the throwable type, so nobody writes their own `static`-counter version. - Ship one shared listener, through a ServiceLoader jar, that records every result with `wasRetried()` and takes evidence for it in `onTestSkipped` or `IInvokedMethodListener.afterInvocation`. - Publish the count of retried attempts per test, and agree a threshold at which the team stops feature work to fix flakes. - Review the opt-in list regularly; a test retried in most runs is broken, not flaky. A shared analyzer can be this small: ```java import org.testng.ITestResult; import org.testng.util.RetryAnalyzerCount; public class NoAssertionRetry extends RetryAnalyzerCount { public NoAssertionRetry() { setCount(1); } @Override public boolean retryMethod(ITestResult result) { return !(result.getThrowable() instanceof AssertionError); } } ``` This version retries any failure except an assertion, once; a stricter policy lists the infrastructure exception types that qualify instead of excluding one. Either way the rule lives in one class that the whole team reviews, rather than in many private copies. ## Pitfalls a lead should name - Approving blanket retry to unblock a release and never removing it. - Measuring suite health by failure count alone, which retries drive towards zero. - Letting each team write its own analyzer, with different caps and a shared `static` counter. - Retrying tests whose failure is an assertion, which re-runs a real bug until it slips through.

  • A product manager wants blanket TestNG retries to unblock a release. How do you respond?
    Accept a time-boxed exception if the risk is understood, but keep it visible: report retried attempts separately, capture evidence for each, and set a removal date. TestNG keeps one transformer, so a blanket one replaces, or is replaced by, any other transformer the suite relies on; check that first.
  • Why restrict a TestNG retry analyzer by exception type rather than retrying every failure?
    An `AssertionError` usually means the product disagrees with the test, so retrying it re-runs a real defect until a lucky pass hides it. Retrying only known infrastructure symptoms keeps assertion failures red on their first occurrence.

  • Retry every failure three times and the suite is stable
  • A green build with retries means the product is fine
  • Assertion failures are as safe to retry as timeouts
  • Retried attempts need no reporting because the test passed
  • Each team should write its own retry analyzer