Speed up your test suite

How to speed up a slow pytest suite

A checklist for slow Python test suites, from the fixes that usually save the most time (password hashing, migrations, test data) to parallelism and CI. Each item links to a detailed guide.

Slow test suites are rarely slow everywhere. Most of the time goes to a handful of causes that repeat across hundreds of tests: hashing the same password, building the same rows, running the same migrations, waiting for the same sleep. Fix one of those and every test that shares it gets faster.

This page is a checklist, roughly in the order the fixes pay off in a typical Django or SQLAlchemy project. Each item links to a guide with the reasoning, before/after code and the caveats that keep your tests correct.

Measure before you change anything

Guessing where test time goes is usually wrong, and a fix you can't measure is a fix you can't defend in code review.

  • Find the slowest tests with pytest --durations=20, then look inside them. See find your slowest pytest tests.
  • Profile the whole suite, not one test: the expensive thing is often spread thinly across every test (a setUp, an autouse fixture, a login). See how to profile a pytest suite.
  • Compare before and after. Run the suite a few times before and after a change; the noise between runs is often bigger than you'd expect. flakytest compare shows which tests got faster or slower, and which changed outcome.

Database setup

The database is where most web application suites spend their time.

Test data

Waiting and external services

Python and library overhead

Parallelism and CI

Parallelism multiplies everything above, so it's worth doing after the fixes that remove work rather than before.

Keep it fast

Suites get slow gradually, one reasonable test at a time. Two habits keep them fast: write tests that don't do more work than they need (see Django testing best practices and pytest fixtures best practices), and watch the suite's time as a trend, per environment, so a regression shows up the week it lands rather than the year after. flakytest.dev collects every CI and local run for exactly that.

Related guides

  • How to profile a pytest suite: Profile slow pytest tests with cProfile, pytest-profiling, py-spy, pyinstrument, Python 3.15's tachyon or flakytest: the commands, what each one shows, and which to use when.
  • Find your slowest pytest tests: List the slowest pytest tests with --durations and --durations-min, read setup vs call vs teardown times, then fix, split or mark slow tests so they stop holding up the suite.
  • Speed up pytest in CI: Speed up slow CI test jobs: cache dependencies with uv or pip, use pytest-xdist on every core, shard with pytest-split, migrate once, and see why CI is slower than local.

Updated October 18, 2026 · Suggest a change