Flaky Tests: What They Are and How to Fix Them
A flaky test passes on one run and fails on the next, with no code change. Flaky tests waste time and make people stop trusting the results.
Common causes
- Waiting for time instead of a sign. A fixed pause like "wait 3 seconds" is too short on a slow day.
- Racing the page. The test moves on before an action has finished, for example before a save is complete.
- Shared data. Two tests use the same record and disturb each other.
- A slow or unstable test site.
How to fix them
- Wait for something visible that proves the step finished, such as a heading or a message.
- Use tools that wait for you. In Playwright,
await expect(locator).toBeVisible()keeps checking until it is true or time runs out. - Give every test its own data, for example a unique name with a random id.
- Open the trace or video of the failed run to see what really happened.
What not to do
Do not raise the number of retries to hide the problem. Retries can help a little, but the real fix is to find the cause.
Want to practise this step by step?
Our free Playwright course teaches it in simple English, one day at a time.
Open the free course
Our free Playwright course teaches it in simple English, one day at a time.
Open the free course