What is a flaky test?
Answer
A flaky test is a test that produces inconsistent results — passing sometimes and failing sometimes without any code changes. Flaky tests are a major problem in CI/CD pipelines: they erode trust in the test suite (engineers start ignoring failures), slow down pipelines (forced reruns), and hide real bugs. Common causes: (1) Race conditions: tests with async operations where timing varies. (2) Order dependence: relies on another test having run first. (3) Shared state: global variables or databases not properly reset. (4) Network/external service dependency: external calls with variable latency or availability. (5) Time/date dependence: tests that compare against current time. (6) Random data: tests using random values without fixed seeds. Fixing flaky tests: add proper waits/retries, mock external dependencies, isolate state, use fixed time (inject a clock), use fixed seeds for random. Track and quarantine flaky tests immediately — don't let them accumulate.