Flaky tests
Нестабильные тесты
Automated tests that sometimes pass and sometimes fail on the same version of the code. Their results can depend on things the test doesn't control, such as timing, test order, or shared data.
Also known as: флакающие тесты
What makes a test flaky?
A test fails in CI. You rerun the same commit, change nothing, and it passes. That inconsistency is what makes a test flaky. It doesn't tell you whether the test, the environment, or the application is at fault. For example, a test that catches a race condition only once in twenty runs is unreliable as a merge check, but the bug it exposes is real.
Where flakiness comes from
- Timing assumptions. A test checks the result of an asynchronous operation before it finishes. This may happen more often on a busy CI worker than on a developer's laptop.
- Shared state. One test leaves a database row behind, or two parallel tests write to the same file. Each test may pass on its own but fail when run with others.
- Uncontrolled inputs. A date-sensitive assertion behaves differently at midnight, or a randomized test fails on an input that the run didn't record.
- External dependencies. A test calls a live service whose response times and data can change between runs.
Example: wait for the outcome
An end-to-end test submits an order and looks for a confirmation. If it sleeps for half a second before checking, the test fails whenever the confirmation takes longer than that, even if the order succeeds. A Playwright assertion can wait for the confirmation itself:
await page.getByRole('button', { name: 'Place order' }).click();
await expect(page.getByText('Order confirmed', { exact: true })).toBeVisible();
The assertion succeeds as soon as the message is visible and fails if it never appears within the assertion timeout. Unlike a longer sleep, it doesn't make fast runs wait unnecessarily. If the requirement is specifically to confirm orders within 500 ms, however, loosening the wait isn't a fix: that timing requirement needs to be tested explicitly.
How to investigate a flaky CI failure
Keep the logs, error output, and any screenshots or traces from the first failure. Rerun the same revision, first in isolation and then as part of the suite. Compare serial and parallel runs if you suspect a shared resource. A test that fails only after a particular neighbor gives you a concrete lead: inspect what that neighbor leaves behind. Fix the cause by isolating fixtures, controlling the clock, recording and replaying a random seed, or correcting the application's synchronization, as appropriate.
A limited retry can keep a pipeline moving while someone investigates, but report the original failure even when the retry passes. If the dashboard shows only the final green result, the team loses the evidence it needs to prioritize the fix.
How it comes up in interviews
You may be asked what you'd do about a test that's green locally but intermittently red in CI. Start with the exact failure, not a guess about the cause. Explain what evidence you'd collect and how you'd test one hypothesis. For example, if two tests create a user with the same email, give each run unique test data and check whether the failure still occurs under parallel execution.