Why Your Tests Are Slow (And How to Fix Them)
A practical guide to diagnosing slow test suites: startup costs, database fixtures, sleeps, e2e scope, parallelism, and the budgets that keep suites fast.
By Velloreth
A test suite that takes twenty minutes does not get run. It gets run in CI, maybe — and skipped locally, trimmed to “the important ones,” quietly reduced to smoke tests on feature branches. The suite’s value comes from being run every time, and speed is what makes that happen. Slow tests are a correctness problem wearing a performance costume.
The fix is almost never “buy faster hardware.” It is finding the repeated cost you are paying thousands of times.
Measure the shape first
Before touching a single test, get the breakdown. Every major runner can emit per-file or per-test timings:
# produce a timing report, slowest tests last
npx vitest run --reporter=verbose 2>&1 | sort -t'>' -k2 -n
npx jest --logHeapUsage --verbose
Then classify what you see. The pattern matters more than the total:
| Symptom in the report | Likely cause |
|---|---|
| Every test ~identically slow | Shared setup paid per test (DB, container, boot) |
| First file slow, rest fast | One-time startup cost — fine |
| A few tests are minutes | External calls, sleeps, timeouts |
| Time grows over the run | Leaked connections, accumulating fixtures |
| CI much slower than local | Cold caches, no persistence, serialization |
| Total time >> sum of test times | Overhead between tests: imports, setup/teardown |
That last row is the sneakiest. Tests themselves may be fast while module loading, database migrations, or environment boot happen once per test file — repeat that across 300 files and the overhead dwarfs the assertions.
The usual suspects
1. Repeated environment startup. Booting an app context, running migrations, or starting a test container once per file (or worse, per test) is the classic tax.
# paid on every single test
def test_something():
db = create_database() # migrations, seed, teardown
...
db.destroy()
# paid once per session
@pytest.fixture(scope="session")
def db():
database = create_database()
yield database
database.destroy()
@pytest.fixture(autouse=True)
def clean_tables(db): # cheap: truncate, not recreate
db.truncate_all()
yield
The two-level fixture — heavy object built once, cheap isolation per test — is the single most effective structural fix for data-driven suites. Containers and app contexts work the same way: start one, reuse it, clean between tests.
2. The database. Beyond fixtures, look for:
- Network round trips per assertion. Ten queries per test across a real server is ten times the cost of one in-process call. For unit-level tests, an in-memory repository implementing the same interface removes the server entirely.
- Oversized fixtures. Seeds that create 500 rows when the test reads three. Build the minimum data that makes the assertion meaningful.
- Connection storms. A new connection per test instead of a pooled one — check the pool settings your test config actually uses.
- Real databases where fakes suffice. Reserve the real engine for tests that assert engine-specific behavior (constraints, transaction isolation, SQL dialect). Everything else can run against the interface.
3. Sleeps and polling. sleep(2) “to let it finish” is a permanent 2-second tax on every run, and it is still flaky — you have paid for certainty you did not get.
# slow AND flaky
time.sleep(2)
assert worker.result() is not None
# fast and deterministic
result = wait_for(lambda: worker.result(), timeout=5)
assert result is not None
Prefer, in order: synchronizing on the actual completion signal (a future, a callback, a file lock), polling with a short interval and a real timeout, or injecting a fake clock for anything time-dependent. A test that sleeps does not wait for the work; it waits for your worst-case guess.
4. Tests that build the whole world. Using the full HTTP stack, real auth, and a browser to assert that a validator rejects an empty field is a category error. Test that at the unit level, where it costs milliseconds; reserve the full stack for the wiring between components.
| Layer | Scope | Target per-test cost |
|---|---|---|
| Unit | One function/class, fakes for I/O | Microseconds–milliseconds |
| Integration | One module + its real collaborators | Tens of milliseconds |
| System / API | Full app over HTTP, real DB | Hundreds of milliseconds |
| E2E / browser | Real browser, real flows | Seconds |
Total suite time is dominated by whichever layer you use for everything. A pyramid skewed toward the bottom runs fast because cheap tests are cheap, not because anything was skipped.
5. E2E scope and serialization. Browser tests are expensive by nature. Contain them: run only the critical journeys per commit, keep them on independent pages and isolated users, and parallelize across workers with genuinely isolated state (separate accounts, no shared cart). Retries should be rare — every retry is a slower suite hiding a bug in the suite.
Parallelism: the multiplier you earn
Sharding turns a 15-minute serial run into a 3-minute one across four workers — but only when tests do not depend on each other. Before enabling parallelism, hunt the order dependencies:
- Shared files, ports, or environment variables mutated by tests.
- A single database schema all workers write to.
- Global singletons that leak between test files.
- Tests that assume “the cache is empty” at start.
Each of those is a bug in the tests regardless of speed, and parallel runners simply surface them faster. Typical setups shard by file (--shard=1/4 in Jest and Vitest, or CI-level splitting across runners) and keep each worker’s data isolated by construction.
Also worth checking: whether CI is re-doing work your laptop caches. Dependency installation without cache persistence, cold Docker layers, and re-running migrations on every job are pure CI-only overhead. Warm caches and shared build artifacts are low-effort wins with no effect on correctness.
Flakiness is a speed problem too
The economics are brutal: a flaky test triggers retries, each retry costs full runtime, and every occurrence pulls a human in to investigate. A suite with a 1% flake rate across a thousand tests produces failures constantly — and the response is almost always to slow things down further (more retries, longer timeouts).
Fix flakes at the root, usually one of four causes:
- Sleeps / insufficient synchronization — replace with real waits.
- Shared mutable state — give each test its own data.
- Time and randomness — inject fixed clocks and seeded generators.
- Real external services — stub them, or mark those tests for a scheduled run rather than every commit.
A good health metric: track retry counts as a first-class CI number. Time-to-green that includes retries is your real suite duration.
Structure: run the right tests at the right time
Not every test belongs on every trigger. A three-tier setup keeps the inner loop fast without sacrificing coverage:
| Trigger | What runs | Budget |
|---|---|---|
| Every save / pre-commit | Affected unit + component tests | Seconds |
| Every push / PR | Fast layers + integration for changed areas | Under ~2–3 min |
| Merge to main / nightly | Full suite, E2E, slow and flaky-prone categories | 10–15 min max |
Modern runners make the middle tier practical: run only tests affected by changed files, tag suites (@slow, @integration, @e2e) and select by tag, and keep known-slow tests out of the default local command so people actually run the default.
The complementary discipline is deleting redundant coverage. Two tests asserting the same behavior through different layers cost double and protect no more than one — choose the layer where the failure is cheapest to diagnose (usually the lowest one that still exercises the real code).
A worked example of the diagnosis
Take a hypothetical but very ordinary suite: 18 minutes on CI, 9 minutes locally, 400 tests. The timing report might read:
total 18m 02s
setup/teardown 11m 40s <- migrations + app boot, once per FILE
tests 5m 10s
unaccounted 1m 12s <- imports, env loading, worker idle
The tests themselves are five minutes. Everything else is overhead paid repeatedly, which means three changes move most of the number:
- Session-scoped environment — boot the app and run migrations once per worker instead of once per file. Overhead drops from tens of seconds per file to one-time.
- Cheap isolation — truncate tables between tests instead of recreating the schema. What was a per-test migration becomes a delete.
- Deeper sharding — with per-test setup mostly gone, tests can split four ways without stepping on each other; wall-clock time roughly quarters.
Nothing about the assertions changed, no test was deleted, and the suite goes from “people skip it locally” to part of the inner loop. That ratio — overhead outweighing assertions — is more common than not, which is why the first action after a slow-suite complaint should always be a timing report rather than an opinion about which tests to remove.
It is also worth asking why local runs are twice as fast as CI. Common answers: cold caches for dependencies and Docker layers, no persistence between jobs, fewer parallel workers, or CI running a superset of what developers run. Each has a targeted fix (cache directories keyed on lockfiles, reused build artifacts, matching worker counts), and none of them involves touching a test.
Delete, dedupe, or demote
Before optimizing a slow test, ask what it is actually protecting. Three honest outcomes:
- Delete. It duplicates coverage that a faster test already provides at a lower layer — a browser test asserting a field-level validation rule that the unit test covers in a millisecond. Redundant coverage inflates runtime without inflating safety.
- Dedupe. Two tests differ only in data fixture; parameterize them into one test with a table of cases instead of two copies of the same body.
- Demote. The test is valuable but slow — move it to the merge or nightly tier rather than the commit gate. Coverage preserved, inner loop kept fast.
The test you should keep at all costs is the one that catches an expensive regression: it exercises real wiring, it has caught a real bug before, or it guards an invariant no cheaper layer checks. Runtime is the cost; the failure it prevents is the value. Compare the two before cutting — a suite that is fast because it stopped testing anything is the worst outcome of all.
Budgets keep it fast
Suites do not stay fast by good intentions; they stay fast by regression pressure:
- Set a budget per layer (e.g., unit suite under 60s, PR gate under 3 min) and publish the actual number on every run.
- Fail the build when the trend regresses past the budget — not on a single noisy run, but on a rolling window.
- Track slowest-test reports over time. A test that crept from 2s to 20s over six months is visible in a chart and invisible in a code review.
- Quarantine, then fix or delete. A known-flaky test should move to a separate job that still runs but does not block merges — with an owner and an expiry date, not a permanent home.
The number to remember: the cost of a suite is not its runtime, it is its runtime plus the work people avoid doing because it is slow. Getting the gate under a couple of minutes does not save two minutes — it changes whether the safety net is used at all.