← Back to libraryQuestion 280 of 468
🔬TestNG & JUnitAdvanced

Retrying Failed Tests (IRetryAnalyzer)

📌 Definition:

TestNG can automatically re-run failed tests via a custom IRetryAnalyzer, so intermittent (flaky) failures get another attempt before being reported as failed. JUnit 5 does this via extensions or the RetryingTest third-party support.

📖 Detailed Explanation:

In TestNG you implement IRetryAnalyzer's retry(ITestResult) to return true up to N times, and attach it with @Test(retryAnalyzer = MyRetry.class) or globally via a listener/IAnnotationTransformer. A passing retry is reported as passed (sometimes flagged). This absorbs irreducible environmental noise, but — like Cypress retries — it must not MASK real, reproducible bugs. JUnit 5 has no built-in retry; you use an extension (e.g. @RepeatedTest is different; retry needs a custom TestExecutionExceptionHandler or a library). Best practice: fix root causes (waits, stable selectors, isolated data) and use retries sparingly as a safety net.

🔑 Key Points:
  • TestNG IRetryAnalyzer.retry() re-runs a failed test up to N times
  • Attach per-test (retryAnalyzer=) or globally via a listener/transformer
  • JUnit5 needs an extension/library (no built-in retry)
  • Don't let retries mask reproducible bugs — fix root causes
🌍 Real-World Example:

A flaky UI test tagged with a retry analyzer (2 retries) stops failing the nightly build on transient network blips, while the team tracks and fixes the underlying wait/selector issues rather than relying on retries.

🎯 Scenario-Based Interview Question:

Your CI is only green because failed tests retry 3 times. Why is that risky and what's the correct long-term action?