Flaky Tests

A flaky test is a test that passes in one run and fails in another even though nothing in the code or test has changed.

Table of Contents

Table of Contents

Further Reading

No items found.
No items found.
No items found.
No items found.
No items found.
No items found.

What Does a Flaky Test Mean?

A flaky test is a test that cannot be trusted to give consistent results. It may pass in one run and fail in the next. Nothing in the code or tests changed in between.

This inconsistency undermines confidence in test automation and CI/CD pipelines. It also slows releases, because engineers must rerun tests and investigate false failures before deploying.

Common causes of flaky tests include:

  • Timing and synchronization: race conditions, short waits for async work, hard-coded sleeps, or animations that finish at unpredictable times.
  • External dependencies: third-party APIs, databases, or queues with variable latency, rate limits, or outages.
  • Unstable environments: resource contention, CPU or memory pressure, leftover state, or shared data changed by parallel runs.
  • Order dependency: one test affects another, so results change when the run order changes.
  • Network unreliability: timeouts, DNS delays, proxy issues, or short network partitions.
  • Non-deterministic data: tests rely on current timestamps, unseeded random numbers, or real data that changes.
  • Browser and UI timing: render delays, JavaScript timing, DOM race conditions, or viewport-specific behavior.

Because failures seem random and are hard to reproduce, flaky tests cause confusion. Over time, teams stop trusting automated testing.

Flaky Test vs. Broken Test vs. Flaky Infrastructure

Type Behavior Likely cause First step
Flaky test Passes and fails with the same code Timing, shared state, or test design Isolate and stabilize the test
Broken test Fails every time under the same conditions Real bug or intended app change Fix the app or update the test
Flaky infrastructure Many unrelated tests fail at once Cluster, node, or network instability Check cluster events and resources

See flaky infrastructure for a deeper look at environment-driven instability.

Why Flaky Tests Matter

Flaky tests add uncertainty to CI/CD pipelines. When engineers cannot tell a real bug from random noise, triage takes longer and release confidence drops.

Even a small share of flaky results causes wide inefficiency. Teams rerun tests, suppress unstable ones, or bypass test gates to keep shipping.

The broader impacts include:

  • Lost productivity: engineers switch context to investigate failures that are not real.
  • Less confidence: teams ignore real failures or disable tests altogether.
  • Slower deployments: teams wait for reruns or manually confirm results.
  • Higher costs: unnecessary retries waste CI/CD compute.
  • Frustration and burnout: unreliable tooling and false alarms wear teams down.
  • Shipped bugs: teams grow numb to failures and stop investigating carefully.

Reliable pipelines must separate true failures from environment-driven flakiness. That is where Testkube adds value.

A B2B SaaS Example

Picture an expense management SaaS company whose receipt-upload test fails about once a week. Developers learn to click "rerun" and move on.

One week, the failure is real. A storage bug drops large receipts for a key customer. Because the team had stopped trusting that test, the bug reaches production.

Our Perspective: Quarantine, Then Fix

Leaving a flaky test in the main suite teaches the team to ignore red builds. Deleting it removes coverage. Neither is a good answer.

A better pattern is to quarantine the test in a separate, non-blocking run. Track it there, fix the root cause, and then return it to the main suite.

Flaky Tests and Testkube

Testkube does not analyze or automatically repair flaky test code. It does help teams find and reduce flaky infrastructure, which is a common root cause. Many tests look flaky because of environment inconsistencies, not test logic.

Testkube runs each test in an isolated Kubernetes pod with clean state. This reduces noise from resource contention, leftover data, and cluster drift.

Testkube helps teams:

  • Detect infrastructure instability: separate environment issues from real test problems.
  • Compare runs: check outcomes across consistent environments to see if flakiness is environment-specific.
  • Match failures to events: see if failures line up with pod restarts, node issues, or resource exhaustion.
  • Cut false alarms: remove failures caused by scheduling delays or resource problems.
  • Track history: use execution history to tell consistent failures (real bugs) from intermittent ones (flakiness).

How Testkube Reduces Flaky Behavior

  • Ephemeral pods: each run gets its own pod with clean state, which prevents carryover from earlier runs.
  • Kubernetes context: pod status, events, and logs are kept with each execution to reveal environment issues.
  • Resource control: set resource requests, limits, node selectors, and tolerations to prevent starvation and timeouts.
  • Central logs and artifacts: compare flaky runs side by side using stored logs and artifacts.
  • Production-like execution: tests run inside Kubernetes, close to how the app runs in production.
  • Parallel isolation: parallel runs stay separate, avoiding the shared-state conflicts common in traditional CI.
  • Retries and timeouts: Test Workflows support step retries and timeouts for known transient issues.

When a test fails on and off, teams can quickly find the source. It may be external, like node or network problems. Or it may be internal, like poor waits or race conditions.

Frequently Asked Questions (FAQs)

What percentage of flaky tests is acceptable?

Ideally, zero. Even a small amount of flakiness compounds quickly in large suites. Monitor flakiness continuously and fix it as a priority.

How do I identify which tests are flaky?

Track results over time and look for tests with mixed outcomes on the same code. Testkube execution history shows which tests fail on and off versus every time.

Should I retry flaky tests automatically?

Retries can hide flakiness without fixing it. Use them sparingly for known transient issues like network timeouts. Track retry rates and investigate tests that need frequent retries.

What is the difference between a flaky test and a broken test?

A broken test fails every time under the same conditions, which points to a real bug or intended change. A flaky test fails unpredictably, which points to timing, environment, or test design issues.

Can infrastructure cause tests to be flaky?

Yes, and it is one of the most common causes. Resource contention, network instability, node failures, and cluster issues can all make tests fail on and off.

How do I fix flaky tests?

Replace hard-coded sleeps with explicit waits, isolate and clean up each test, and use fixtures instead of shared state. Add retries for external dependencies and run tests in isolated environments.

Related Terms and Concepts

No items found.