12 / 57 · 26 Testing Like a Senior · Assertions and Flaky Tests← prev⊞ allnext →☰ Read as one page
3.3Flaky Tests and Deterministic Design
A flaky test is a test that passes and fails without any code change. Flaky tests destroy trust in the test suite — when a failure might be "just flakiness," developers stop investigating failures.
Root Causes of Flakiness
| Source | Example | Fix |
|---|---|---|
| Timing / race conditions | Asserting before async operation completes | Use event-driven waits, not sleep() |
| Shared state | Test A creates data that Test B depends on | Isolate test data per test |
| External dependencies | Test calls a live third-party API that is slow or down | Mock external services |
| Order dependency | Tests pass in sequence but fail when shuffled | Each test must set up its own preconditions |
| Non-deterministic data | Asserting on timestamps or random IDs | Assert on stable properties, use patterns/ranges |
Anti-Pattern: Add retries to make flaky tests pass. The test still has a bug — you have just hidden it behind retries.
Pattern: Design tests for determinism from the start. When flakiness appears, investigate and fix the root cause. Track flaky test rates on a dashboard — a rising flaky rate is an early warning that test architecture needs attention.