QA Interview Hub · Knowledge base · Quizzes · Українська

System Design › Stability and flaky tests

Retries: when they help and when they hurt

A retry restarts a failed test from the beginning in a new worker, and if it then passes, its status is flaky. It is a fallback for random failures, not a fix. It helps with infrastructure failures and collecting traces, but hurts when it hides a real bug, such as a race condition in a debit. Waiting for a condition, like expect.poll, is synchronisation that a retry does not replace.

System design · Retries

A retry turned the pipeline green. What did it hide: an unstable test or a money bug?

The config has retries: 2, the pipeline is green, and the transfer test is marked as flaky. The team got used to it: "passed on the second attempt, so it is fine". In reality one run in ten debits the money twice, and the retry neatly repaints red as green. A retry is a useful tool when you know what it does and what it does not. The lesson covers where it helps, where it hurts and how it differs from waiting for a condition.

How it works and why. If a test fails and retries is above zero, Playwright discards the worker the test failed in, starts a new process with a clean browser state and runs the test from the beginning. There are three final statuses: passed (on the first attempt), flaky (failed, then passed) and failed (failed on all attempts). A fresh worker is needed so that the bad state of the previous attempt does not leak into the next. This is both the strength and the weakness: a test that depends on random state or time may pass on a retry simply because the conditions changed.

What a retry does in Playwright

Attempt 1: failedthe worker is discardedA new workerclean browser, test from scratchAttempt 2: passedstatus flaky, run is green
Figure 1. A green run does not mean there was no problem: the flaky status is the signal.

When a retry helps.

  • As temporary protection against infrastructure failures in CI: an overloaded agent, a network failure while loading dependencies.
  • For collecting diagnostics: trace: 'on-first-retry' records a trace of the first retry, so normal runs do not spend time on traces and one appears for a failed test. If you need the trace of the failure itself, use 'retain-on-failure' or 'retain-on-first-failure'.
  • As a source of instability data: the flaky status goes into history and metrics.

When a retry hurts.

  • When it hides a product bug. A race condition in debiting may show in one run out of ten. A retry makes the pipeline green and the bug goes to production.
  • When retries are large (3-5): run time grows while stability does not improve.
  • When the test repeats a non-idempotent action not only in the browser but also through the API: a repeated POST of a transfer may create a second transfer in the same database if the test does not clean up.

Example: a retry versus a wait. The transfer is processed asynchronously and the test checks its status. The wrong approach is to rely on a retry of the whole test, because the first attempt often checks the status before processing.

// before: relies on retries: 2 to hide the race with the queue
const res = await api.getTransfer(id);
expect(res.status).toBe('COMPLETED');

The right approach is waiting for a condition inside the test. expect.poll repeats the function at intervals until the value matches or the timeout runs out:

await expect
  .poll(
    async () => {
      const res = await api.getTransfer(id);
      return res.status;
    },
    { timeout: 15_000, intervals: [500, 1000, 2000] },
  )
  .toBe('COMPLETED');

The difference is fundamental. A wait is a normal part of the test: the system is asynchronous by design and the test waits for the final state. A retry is a restart of the whole test from scratch: a fallback for random failures. The first is correct synchronisation, the second is insurance. If a test without a wait passes only on a retry, it must be fixed, not left on a retry.

Configuration. Globally a small retry only in CI, and for a group where instability is known and the cause is already in a ticket it can be changed separately:

// playwright.config.ts
export default defineConfig({
  retries: process.env.CI ? 1 : 0,
  use: { trace: 'on-first-retry' },
});

// one group only
test.describe('payments sandbox', () => {
  test.describe.configure({ retries: 2 });
});

Locally retries are kept at zero: the developer should see a failure at once. Inside a test the attempt number is available as testInfo.retry, but branching test behaviour on it is not advisable: a test that behaves differently on a retry no longer checks the same thing.

Limits and alternatives. A test-level retry does not cure the cause, it only lowers the chance of red. For external dependencies (a payment sandbox) a mock or stub is more reliable than a repeat. For a blocking gate on money many teams forbid relying on a retry: the --fail-on-flaky-tests flag makes a run red if a test passed only on a retry. Cypress has a similar mechanism, test retries (retries in the config, separately for runMode and openMode); in the Selenium world retries come from the test runner or a plugin, and the risks are the same.

Common mistakes.

  • Relying on a retry instead of a wait: the test passes "the second time" and stays wrong.
  • Ignoring the flaky status in the report: a green run creates a feeling that all is well.
  • Setting retries to 3-5 "for reliability": the pipeline gets slow and instability grows unnoticed.
  • A retry on a test that creates records without cleanup: a repeat leaves duplicates.

Check yourself

The transfer test passes only on the second attempt in 1 of 10 runs, retries: 2. The pipeline is green. What next?

Answer

The flaky status is a signal, not a success. We check in the trace whether it is synchronisation in the test or a wrong result in the product (for example a double debit). If the test simply does not wait for asynchronous processing, we add expect.poll. If the result is wrong, we file a defect. We do not remove the retry until the cause is understood, and we do not consider the problem closed either.

How does trace: 'on-first-retry' differ from 'retain-on-failure' and when to use which?

Answer

on-first-retry records a trace only on the first retry: runs without failures pay nothing for recording, and the retry trace shows behaviour after the failure. retain-on-failure records a trace of every attempt and keeps the ones that failed: it is the failure itself, but recording always runs. For normal CI the first is used, and for investigating a specific unstable test the second.

In the interview. Junior: A retry reruns a failed test and if it passes the status is flaky. It helps with infrastructure failures but does not replace fixing the cause. Middle/Senior: I set one or two retries in CI and always track tests that pass only on a retry, because in fintech a retry can hide a race condition in debiting. Waiting for a condition (expect.poll, web-first assertions) is synchronisation inside the test, and a retry is a fallback at the level of the whole test; the two must not be confused.

More articles in this chapter