Flaky Tests

Published on February 16, 2025

The term flaky test refers to a test that sometimes passes and sometimes fails without changes to the code. These tests are unreliable and can make debugging difficult.

Common Causes of Flaky Tests

1. Timing Issues

Tests depend on the process to be completed within a certain time.

Example: A test assumes a network request completes instantly.

2. Concurrency & Race Conditions

Tests fail due to multiple threads or processes accessing shared resources in an unpredictable order.

3. External Dependencies

Tests rely on databases, APIs, or third-party services that may be slow, unavailable, or inconsistent.

4. Random Data or Order-Dependent Tests

Tests generate random values or depend on previous test states, leading to inconsistent results.

5. Environment Differences

Tests behave differently due to OS, hardware, or configuration differences.

How to Fix Flaky Tests

✅ Use Mocks & Stubs — Replace external dependencies with test doubles.
✅ Add Explicit Waits — Use explicit conditions instead of fixed time delays.
✅ Run Tests in Isolation — Ensure tests don’t depend on the state of others.
✅ Make Tests Deterministic — Avoid random data that changes between runs.
✅ Log & Retry — Capture test failures and retry tests intelligently.