A test suite that takes 40 minutes to run stops being a safety net and starts being a tax. Developers skip running it locally, pull requests pile up waiting for CI, and someone eventually suggests the worst fix of all: delete tests until the suite is fast again. That trades real coverage for a green build, and the bugs those tests would have caught come back as incidents.
Learning how to speed up a slow test suite without deleting tests is a better trade. Most slow suites are slow for a handful of fixable reasons: tests run one after another, every test rebuilds the world from scratch, real network calls sneak into unit tests, and the database does far more work than any single test needs. The slow test suite fixes below keep every test and cut the wait.
How to Speed Up a Slow Test Suite: Start by Measuring
Guessing which tests are slow wastes the afternoon. Run the suite with a timing report first. Jest has --verbose and slow-test reporters, pytest has --durations=20, and most runners can print the slowest files. The report usually shows the same pattern: a small number of tests eat most of the time.
Measure the whole suite too, not just individual tests. If one file is quick alone but the suite takes 40 minutes, the problem is setup, not the tests themselves. If the suite is fast locally and slow in CI, the problem is the CI environment, and the CI build itself may need the same kind of treatment before you touch a single test.
Write the numbers down: total runtime, slowest ten tests, time in setup. Every fix below should be judged against that baseline. A fix that feels faster but does not move the number is not a fix.
Run Tests in Parallel
The single biggest win for most suites is parallelism. A suite that runs 800 tests one at a time on a machine with eight cores is leaving most of the machine idle. Most runners support it: Jest runs test files in parallel by default, pytest needs the pytest-xdist plugin, and Go runs package tests concurrently. Turning on parallel test execution can cut wall-clock time by half or more on a multi-core machine, with no test changes at all.
Parallelism only works if tests do not fight over shared state. Tests that write to the same database tables, the same files, or the same ports will collide when they run side by side. Give each worker its own database or schema, use temporary directories instead of fixed paths, and let the runner assign ports.
In CI, match the worker count to the machine. A runner with two cores gains nothing from eight workers; the extra processes just compete for CPU. Start with one worker per core, measure, then adjust.
Cut the Setup Cost
Look at what happens before the first assertion runs. Many suites boot the whole application, load every module, and seed a database for every single test file, even for tests that only check a pure function. That setup can cost seconds per file, and seconds times hundreds of files is the suite.
Split setup into layers. Heavy setup, like starting a database or loading a framework, belongs in a session-level fixture that runs once. Only the data a specific test needs should be created inside the test itself. When a test only needs a small helper, it should not pay for the full application boot.
Factories beat fixtures here. A factory that builds one user in memory is cheaper than a fixture file that inserts fifty rows the test never reads. Create the minimum data the test asserts on, and nothing else.
Replace Real Services with Fakes
A unit test that calls a real payment API, sends a real email, or hits a real third-party service is not a unit test. It is slow, it depends on the network, and it fails when the other service has a bad day.
Put a boundary between your code and the outside world, then fake the boundary in tests. A fake payment gateway that returns a canned success response runs in microseconds. A fake clock lets you test expiry logic without waiting. The test still checks your logic; it just stops checking someone else’s service at the same time.
Keep a smaller set of integration tests that hit the real services, and run those separately. The point is not to stop testing the integration. It is to stop paying the network cost in every test that does not need it. Integration tests have a clear job, and it is different from the job of a unit test, so let each kind of test run where it belongs.
Shrink the Database Work
Database tests are usually the slowest group in a suite, and the fixes are unglamorous. The first is to stop resetting the whole database between tests. Dropping and recreating tables, or truncating every table, for each test is expensive. Wrapping each test in a transaction and rolling it back afterwards gives the same isolation at a fraction of the cost.
The second fix is to use fewer rows. A test that checks pagination needs maybe eleven rows, not ten thousand. Seed data should be sized to the assertion, not to realism.
The third fix is to run database tests against a fast local database, not a remote one. A test database on the same machine removes network round trips from every query. Keep the production-like database for a nightly run, and let the fast feedback loop use the fast setup.
Split the Suite and Run Only What Changed
Not every change needs the whole suite. Splitting the suite into groups, by module, by speed, or by test type, lets CI run the relevant group on every pull request and the full suite on a schedule or before merge.
Test selection tools make this automatic. They map changed files to the tests that cover them and run only those, plus a safety margin of related tests. The full suite still runs, just less often, while the fast subset gives feedback in minutes instead of half an hour.
Keep a fast suite and a slow suite as explicit groups. The fast group, mostly unit tests, should finish in a few minutes and run on every push. The slow group, integration and end-to-end tests, runs before merge and on the main branch.
Fix Flaky Tests Instead of Rerunning Them
A flaky test that fails one time in five costs more than its runtime. CI reruns it, developers rerun the pipeline, and everyone learns to ignore red builds, which hides real failures.
The usual causes of flaky tests in CI are timing assumptions, shared state, and ordering dependence. A test that sleeps for a fixed 500 milliseconds and hopes a background job finished is a race, not a test. A test that passes alone but fails in the full suite is reading state left behind by another test. Both are fixable: wait for a condition instead of sleeping, and give each test its own data. Debugging a flaky test starts with reproducing it reliably, and the most reliable reproduction is usually running the failing test together with the test that runs before it.
Quarantine a flaky test only as a short-term measure, with a ticket attached and a deadline. A quarantined test with no owner and no deadline is a deleted test with extra steps.
Keep the Suite Fast After the Fix
Speed work rots without a guardrail. Add a time budget to CI: fail the build if the fast suite passes a threshold, or post the suite runtime as a comment on each pull request so a slowdown is visible before merge. A slow test then shows up the day it lands instead of six months later.
Review new tests the way you review new code. A test earns its place by catching a real bug, not by adding a line to a coverage report, so ask whether a new test needs the database, the network, or a full application boot before approving it. The cheapest slow test is the one that never gets written.
Revisit the slowest-tests report once a month. Suites drift: a new fixture here, a real API call there, and test suite performance creeps back up. Fifteen minutes a month keeps the suite near its best time, and it beats another big speed project next year. If you are choosing a runner, the differences in speed between test runners are real, but they matter less than the structure of the tests themselves. Fix the tests first, then pick the tool.
The goal was never a shorter suite. It is a suite developers actually run. Measure first, parallelize, cut setup, fake the network, shrink the database work, split the run, and fix the flakes. The tests stay, the coverage stays, and the waiting goes away.