Skip to content
Velloreth
Programming 10 min read

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

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 reportLikely cause
Every test ~identically slowShared setup paid per test (DB, container, boot)
First file slow, rest fastOne-time startup cost — fine
A few tests are minutesExternal calls, sleeps, timeouts
Time grows over the runLeaked connections, accumulating fixtures
CI much slower than localCold caches, no persistence, serialization
Total time >> sum of test timesOverhead 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.

LayerScopeTarget per-test cost
UnitOne function/class, fakes for I/OMicroseconds–milliseconds
IntegrationOne module + its real collaboratorsTens of milliseconds
System / APIFull app over HTTP, real DBHundreds of milliseconds
E2E / browserReal browser, real flowsSeconds

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:

  1. Sleeps / insufficient synchronization — replace with real waits.
  2. Shared mutable state — give each test its own data.
  3. Time and randomness — inject fixed clocks and seeded generators.
  4. 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:

TriggerWhat runsBudget
Every save / pre-commitAffected unit + component testsSeconds
Every push / PRFast layers + integration for changed areasUnder ~2–3 min
Merge to main / nightlyFull suite, E2E, slow and flaky-prone categories10–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:

  1. 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.
  2. Cheap isolation — truncate tables between tests instead of recreating the schema. What was a per-test migration becomes a delete.
  3. 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:

  1. Set a budget per layer (e.g., unit suite under 60s, PR gate under 3 min) and publish the actual number on every run.
  2. Fail the build when the trend regresses past the budget — not on a single noisy run, but on a rolling window.
  3. 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.
  4. 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.

Frequently asked questions

How long should a test suite take?
The inner loop — unit and component tests developers run constantly — should stay under a couple of minutes. The full merge gate can take longer, ideally under ten to fifteen. Beyond that, people start running fewer tests, which is worse than a somewhat slower but complete suite.
Should I delete slow tests instead of speeding them up?
Only after checking what they catch. First shrink their scope, isolate them as integration tests, and run them on a slower cadence. Deleting a slow test that guards a real regression trades minutes for outages; deleting one that duplicates coverage is a win.
Do flaky tests make suites slow?
Directly and severely: retries multiply runtime, and the investigation time around each flake dominates everything. Fixing flakiness — usually by removing sleeps, shared state, and order dependence — is often the single biggest speed improvement available.
Is parallelism the answer to a slow suite?
It is the answer to suites that are slow because of scale. Parallelism does nothing for a suite bottlenecked on one shared database or serialized browser, and it can make flaky, order-dependent tests fail more often. Fix the serial bottlenecks first, then shard.