Intermittent Test Failures: What Stopped the Run?
In this article
- What "Stopped the Run" Actually Means
- Why Tests Fail Intermittently
- How to Confirm It's Flaky, Not a Real Bug
- Common Misconception: The Last Failing Test Is the Cause
- How to Fix a Flaky Test
- How to Choose the Right Fail-Fast Setting
- Costs of Ignoring Flaky Tests
- Alternatives to Fixing Flaky Tests
- Frequently Asked Questions
Intermittent test failures are caused by a flaky test, not by a change in your code, and the run itself is stopped by the test runner's failure threshold or a timeout. The specific "stopped the run" message you see is almost always the last failing test before the runner aborted, but the root cause is usually environmental or timing-related, not a logic bug.
What "Stopped the Run" Actually Means
When a test suite runs, the runner (like Jest, pytest, or JUnit) executes tests in a defined order. If a test fails, the runner records it and continues unless you've configured it to stop on the first failure. The message "stopped the run" typically appears in one of two scenarios:
- Fail-fast mode: You set
--bail(Jest),-x(pytest), or--fail-fast(Mocha). The runner aborts immediately after the first failing test, and that test's name is what "stopped the run." - Timeout or resource exhaustion: A test hangs, the runner hits its global timeout, or the process runs out of memory. The runner kills the entire suite, and the last test that was executing is reported as the culprit.
Intermittent failures—tests that pass sometimes and fail other times without any code change—are the classic sign that the failure is not caused by your application logic. They are caused by something outside the test's control: a race condition, a shared resource, or an environment issue.
Why Tests Fail Intermittently
Flaky tests fail for a handful of predictable reasons. Understanding these helps you read the "stopped the run" message correctly.
- Race conditions: Two tests run in parallel and both try to write to the same file, database row, or global variable. The second test to write overwrites the first, causing a failure that only happens when the tests run in a specific order.
- Timing assumptions: A test waits 500ms for an animation or network response, but the machine is slow that day and it takes 800ms. The assertion runs too early and fails. On a faster run, it passes.
- Shared state: Tests depend on data left behind by a previous test. If the previous test is skipped or fails, the next test starts with the wrong data.
- External services: The test calls a real API, database, or third-party service. Network latency, rate limits, or a service outage cause failures that have nothing to do with your code.
- Randomization: Tests that use random data or shuffle order can hit an edge case on one run and not another.
When you see "stopped the run," the named test is often just the first one to trip over one of these issues. The real problem is systemic.
How to Confirm It's Flaky, Not a Real Bug
Before you chase a ghost, verify that the failure is actually intermittent. The fastest way is to rerun the exact same commit multiple times.
- Rerun the failed test alone:
pytest test_file.py::test_nameorjest path/to/test -t "test name". - If it passes in isolation, run it 10 times in a loop:
for i in {1..10}; do pytest test_file.py::test_name; done. - If it passes 10 times alone but fails in the full suite, the problem is interaction with other tests.
- Check the CI logs for the exact same commit run on a different machine. If it passes there, the issue is machine-specific.
If the test fails intermittently even in isolation, it's likely a timing or resource issue within that single test. If it only fails in the full suite, it's a shared-state or parallelization problem.
Common Misconception: The Last Failing Test Is the Cause
A very common mistake is to assume the test named in the "stopped the run" message is the one that needs fixing. In fail-fast mode, that's true—the runner stops because that test failed. But in a timeout scenario, the test that was running when the timeout hit might be a slow test that was just unlucky, while the actual problem is a memory leak or a deadlock in a different test that ran earlier.
Another misconception is that intermittent failures are always caused by the test code itself. Often, they're caused by the test environment: a CI runner with too little memory, a shared database that's being accessed by another job, or a clock that's not synchronized. The test is a victim, not the culprit.
How to Fix a Flaky Test
Once you've confirmed a test is flaky, the fix depends on the root cause. There is no single solution, but these approaches cover the vast majority of cases.
- Remove timing dependencies: Replace
sleep(500)with explicit waits for a condition. UsewaitForin Jest,WebDriverWaitin Selenium, orpytest-timeoutwith a generous limit. - Isolate shared state: Use a fresh database, temp directory, or mock object for each test. Clear global variables in
setUporbeforeEach. - Disable parallelization for flaky tests: If a test can't handle parallel execution, mark it to run serially. In pytest, use
@pytest.mark.serial; in Jest, usetest.sequential. - Mock external services: Replace real API calls with mocks or use a tool like WireMock to simulate responses. This removes network variability entirely.
- Increase timeouts: If a test is genuinely slow, give it more time. A 5-second timeout on a test that takes 4.9 seconds on a loaded CI machine is a recipe for flakiness.
After applying a fix, rerun the test many times—at least 20—to gain confidence. A single pass after a fix proves nothing.
How to Choose the Right Fail-Fast Setting
Fail-fast mode is useful for local development but dangerous for CI. When you're working on a feature, you want to know the first failure immediately. But in CI, you want to see all failures so you can fix them in one batch.
- Local development: Use fail-fast to save time. You'll fix the first bug, rerun, find the next, and so on.
- CI pipeline: Disable fail-fast so you get a full report of every failing test. This is especially important for intermittent failures—you want to see the pattern across the whole suite.
- Nightly or stress runs: Run the suite multiple times with fail-fast off to identify flaky tests. Many teams run the suite 3-5 times in a row on a schedule to catch flakes before they hit developers.
The "stopped the run" message is only as useful as your configuration. If you don't understand why the runner stopped, you can't interpret the message correctly.
Costs of Ignoring Flaky Tests
Flaky tests are not just an annoyance; they have real costs that compound over time.
- Developer time: Every time a flaky test fails, a developer must investigate, rerun, and decide if it's real. This can take 10-30 minutes per incident, and it interrupts flow.
- Lost confidence: When tests fail randomly, developers start to ignore failures. They merge code without checking, and real bugs slip through.
- CI queue time: A flaky failure blocks the merge queue, forcing other developers to rebase and rerun. This slows the entire team.
- Masked real bugs: A flaky test can hide a genuine regression. If the test fails 20% of the time, you might dismiss a real failure as another flake.
Many teams set a rule: if a test fails intermittently more than once in a week, it must be fixed or deleted. A flaky test is worse than no test because it erodes trust in the entire suite.
Alternatives to Fixing Flaky Tests
Sometimes fixing a flaky test is not worth the effort. The test might be testing something that's inherently non-deterministic, like a random number generator or a date-based feature. In those cases, you have alternatives.
- Mark the test as flaky: Some runners allow you to tag a test as
@pytest.mark.flakyand auto-retry it. This is a band-aid, but it keeps the suite green while you investigate. - Delete the test: If the test provides no value—it only tests implementation details—remove it. A deleted flaky test is better than a flaky test that blocks everyone.
- Move the test to a separate suite: Run flaky tests in a separate, non-blocking job. They still run, but they don't block merges.
- Rewrite the test: Change what the test asserts. Instead of checking an exact value, check a range or a property. For example, assert that a sort function returns a list that is sorted, not a specific order.
The key is to make a deliberate choice. Leaving a flaky test in place without a plan is the worst option.
Frequently Asked Questions
Why does my test pass locally but fail in CI?
CI machines are typically slower, have less memory, and run tests in parallel with other jobs. Timing-based assertions that pass on your fast laptop fail on a loaded CI runner. Also, CI often runs tests in a different order, exposing shared-state issues that don't appear locally.
Should I just rerun the failed test until it passes?
Rerunning to confirm flakiness is fine, but rerunning to make the suite green is not. If you rerun and it passes, you've hidden the problem, not fixed it. The next developer will hit the same flake. You should rerun to diagnose, then fix the root cause.
What's the difference between a flaky test and a broken test?
A broken test fails 100% of the time because the code or the test is wrong. A flaky test fails sometimes, usually due to timing, environment, or shared state. Broken tests are easy to fix—you know they're broken. Flaky tests are harder because you have to reproduce the failure.
Can I make my tests 100% deterministic?
No. Any test that touches the file system, network, clock, or random number generator has some non-determinism. You can reduce it by mocking and isolating, but you can't eliminate it entirely. The goal is to reduce flakiness to a rate where it rarely bothers anyone.
How many times should I rerun a test to confirm it's flaky?
Run it at least 20 times in a loop. If it fails once in 20 runs, that's a 5% flake rate, which is still too high for a CI pipeline. If it passes 20 times, it might still be flaky under different load, so also run the full suite a few times.