October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk6 min

How to Find and Clean Up Dirty Automated Tests

A practical workflow for identifying flaky or state-leaking tests, reproducing suite-only failures, fixing cleanup and synchronization, and using quarantine responsibly.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A “dirty” automated test is an informal term for a test that depends on uncontrolled prior state, leaves state behind for later tests, or has unreliable setup or cleanup. Find candidates by comparing CI history, isolated runs, and suite runs; then make the test’s required state explicit and its cleanup reliable. A retry can help reveal intermittent behavior, but it does not repair the cause.

What makes an automated test dirty or flaky?

“Dirty test” is not a formal testing standard. Here, it describes a test whose result or side effects depend on state the test does not control. A flaky test is one that passes and fails intermittently without an explanatory change. pytest describes flaky tests as tests with intermittent or sporadic failure and seemingly nondeterministic behavior.

As an Amazon Associate I earn from qualifying purchases.

The terms overlap, but they are not identical: a test can be dirty because it contaminates a later test even when it passes consistently by itself. A changing result is evidence of flakiness, not proof that the test code is at fault. The runner, application, dependencies, operating system, hardware, network, or resource pressure may be involved too.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do I find flaky tests?

Start with CI history

Look for tests that alternate between pass and fail without an explanatory code change, failures recurring in one area, and failures whose occurrence changes with suite order or parallel execution. Keep enough context to compare like with like:

  • Test identifier and failure output, including relevant logs.
  • Commit or revision.
  • Runner and environment details.
  • Whether the test ran alone or in the suite, and whether ordering or parallelism was involved.

pytest recommends attention to intermittent results and state leakage; Google’s testing guidance likewise treats flakiness as potentially arising from multiple layers, not just the test itself.

Compare an isolated run with the suite

  1. Rerun the suspect test on the same commit and environment.
  2. Run it independently, then compare that result with a suite run.
  3. Repeat while changing one relevant condition at a time, such as test order or parallel execution.
  4. Save the output and environment details for each run so a difference can be traced to a condition rather than an unnoticed setup change.

A test that passes alone but fails in the suite points toward interaction, ordering, or shared state. It is a useful lead, not proof of a specific cause. Independent reruns can also expose assumptions about test state that a normal suite run conceals.

Inspect the execution layers

When reproducing the failure, check the test code and data, framework and runner, application and dependencies, and underlying operating system, hardware, network, and available resources. Review timestamps, logs, initialization and cleanup, environment setup, resource use, and synchronization with asynchronous work. This avoids blaming the test prematurely when the trigger is elsewhere.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why does a test pass alone but fail in the full suite?

Tests in a suite share a process, machine, or external environment more often than an isolated run does. One test may leave altered data or global state for another; a test may depend on execution order; or parallel work may collide over a shared resource. Timing and resource pressure can also differ between the two runs.

Treat the difference as evidence of an interaction to isolate. Inspect setup and teardown, shared fixtures and globals, test data, external services, and any resources mutated by multiple tests. Tests that change global state may not be safe to run concurrently. The repair is to remove cross-test dependencies or isolate the shared state—not simply to force a preferred order unless ordering is itself part of the behavior being tested.

How do I stop one test from affecting another?

Make required state explicit

Each test should establish the data and state it needs rather than inheriting them from a previous test or run. Use unique test data where appropriate, reset shared state, and isolate external or shared resources when practical. Avoid reliance on uncontrolled system state.

Make cleanup reliable, including after failures

Use the framework’s fixture, teardown, or resource-management mechanisms so cleanup is structured and runs even when an assertion fails. In GoogleTest, a fresh fixture is created for each test, followed by SetUp(), the test body, and TearDown(). However, a fatal assertion returns from the current function: cleanup statements later in that function may never execute. Put essential cleanup in the teardown mechanism or another scope-safe cleanup path rather than relying on code after a fatal assertion.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Synchronize on behavior, not a guessed delay

For asynchronous work, wait for a meaningful application state and use a suitable timeout. Do not use arbitrary sleeps as a repair. Google Testing Blog author George Pirocanac cautions: “Do NOT add arbitrary delays as these can become flaky again over time and slow down the test unnecessarily.” A delay can make a race less visible without removing it, and can slow every run.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Should I retry or quarantine a flaky test?

Retries and quarantine are containment or diagnostic measures, not root-cause fixes. A retry that passes confirms that the outcome can vary; it does not explain why, and it may delay detection of a genuine regression. Google’s historical account of test quarantine warns that it can mask a real race or product bug.

Choice What it helps with Risk and control
Retry Can help identify an intermittent result and keep a transient failure from blocking a run. A passing retry does not establish that the test or product is correct. Retain and inspect the initial failure rather than treating a later pass as a repair.
Quarantine Can separate an unhealthy test from normal gating while investigation proceeds. It can hide a real defect if forgotten. Keep the test visible, link it to an issue, assign an owner, and review it until the fix is verified.
Root-cause repair Restores deterministic setup, synchronization, isolation, or cleanup. Requires diagnosis, but addresses the failure rather than suppressing its signal.

GitLab documents an issue-linked quarantine approach; that is an example of a team process, not a universal standard. The reviewed sources establish no universal retry count, quarantine duration, or acceptable flake threshold. Choose controls that preserve visibility and responsibility.

When should I rewrite a test at a lower level?

If a broad end-to-end test is fragile or failures are difficult to localize, consider whether a smaller test at a lower level can cover the behavior with faster, more isolated feedback. Google’s test-strategy guidance describes smaller, isolated unit tests as a way to get faster and more reliable feedback. A rewrite is appropriate only if meaningful behavior remains covered; do not remove a test merely because it is inconvenient or intermittently failing.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Or skip the browser setup

If a browser test needs a screenshot artifact, ScreenshotNeo is a screenshot API and MCP server; it can simplify capture, but it does not diagnose or repair a flaky test. Its API accepts a URL and returns an image or PDF. The following cURL request captures a page; see the ScreenshotNeo documentation for API options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

ScreenshotNeo accepts cookie or consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and the response identifies the page verdict and billing status in headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf for AI agents and MCP clients. The Free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots. Sign up free for 1,000 screenshots a month, with no card required.

Troubleshooting common failure patterns

  • It fails only after another test: inspect state, data, globals, and resources the earlier test changes; add explicit setup and dependable teardown.
  • It fails only under parallel execution: check for concurrent mutation of global state or shared resources; isolate them or remove the cross-test dependency.
  • It fails around asynchronous work: synchronize on an observable application condition and set an appropriate timeout instead of adding a fixed sleep.
  • Cleanup is skipped on a failed assertion: move cleanup into the framework teardown or another guaranteed cleanup path. In GoogleTest, a fatal assertion can return before later statements in the test function.
  • A retry passes: keep the original failure visible and use the differing outcomes to investigate; the retry is not evidence that the underlying issue is gone.
  • A quarantined test disappears from attention: reconnect it to a tracked issue, name an owner, and review it until a verified repair restores normal gating.
  • A broad end-to-end test remains hard to diagnose: consider a smaller-level test only after mapping where equivalent behavior coverage will remain.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.