Testing Python with pytest?

pytest suite slow, flaky, or hanging on asyncio? We unstick it.

pytest handles small projects almost automatically and then needs real care once the suite grows: fixture scopes that quietly leak between tests, asyncio event loops that collide with other event loops, and a CI run that takes twenty minutes because the fast tests wait for the slow ones. We trace it through conftest.py, the fixture tree and the collection graph until the suite is honest about what it tests and quick about proving it.

  • Since 2015Fixing technology
  • FreeEstimates
  • US teamReal engineers

pytest.log

still flaky

What it is

pytest at a glance

pytest is the de facto testing framework for Python. It was created by Holger Krekel in 2003 (originally as py.test inside the py library), spun out into its own project, and now powers the test suites of most Python codebases in the wild. The current line is pytest 8.x, released in 2024 and maintained actively through 2025 and into 2026.

The framework is open source under the MIT license. Its core distinguishing features are a plain assert statement that still produces rich introspection, a fixture system with scopes (function, class, module, package, session), parametrization at test and fixture level, and a large plugin ecosystem covering coverage, parallelism, asyncio, Django, FastAPI, mocking, and property-based testing via Hypothesis.

Type
Python testing framework
First released
2003 as py.test, 2015 as pytest 2.8, 2024 as pytest 8
License
MIT, open source
Maintained by
pytest-dev team and contributors
Typically used for
Unit tests, integration tests, Django and FastAPI test suites, parametrized data checks, coverage reports

The honest version

pytest pros and cons

No technology is all upside. This is where pytest earns its place and where it tends to cause trouble.

Where pytest is strong

  • Readable failure outputPlain assert statements produce rich comparisons: which key is missing, which element differs, which line of a long string diverges. No assertEqual noise, no custom matchers required.
  • A well-designed fixture systemScopes, dependencies, finalization and parametrization fit into one cohesive model. A well-built conftest.py can set up a whole test environment without any magic in the test files themselves.
  • Parametrization is first classOne test function, twenty rows of input, twenty reported test cases. It replaces entire helper modules of looping and manual labeling with a single decorator.
  • Large plugin ecosystempytest-cov, pytest-xdist, pytest-asyncio, pytest-django, pytest-mock and pytest-benchmark cover most of what a professional Python test suite needs without custom infrastructure.
  • Strong Django, FastAPI and Flask integrationsFramework-specific plugins give you authenticated clients, database transactions per test, and app factory fixtures without having to reinvent them.
  • Scales from one file to a thousandThe same tool runs a weekend script and a full regression suite. The knobs are available when you need them and invisible when you do not.

Where pytest bites

  • Fixture scope mistakes are quietA session-scoped database fixture used from a function-scoped client fixture passes the first run and breaks the second. The failure is often blamed on the database rather than the scope mismatch that actually caused it.
  • asyncio integration is a moving targetpytest-asyncio has changed its event_loop fixture behavior across majors, and tests that used to work now warn or hang. Teams that update Python and the plugin on different schedules feel this most.
  • Conftest magic can get out of handDeeply nested conftest.py files, autouse fixtures that run everywhere, and plugin hooks that modify collection all make the suite hard to reason about once nobody remembers why a given fixture exists.
  • Parallelism is bolted on, not nativepytest-xdist works well most of the time, but tests that share a database, a port or a global cache need explicit coordination. The failure mode is almost always a flake on CI, never a warning.
  • Collection can be slow on huge reposA monorepo with many top-level packages spends real time importing modules before the first test runs. Nothing is broken; the run just starts a minute later than it should.
  • Error messages for plugin issues can misleadAn INTERNALERROR from a broken plugin or a fixture import cycle lands as a stack trace that points to pytest internals, which sends newer engineers hunting in the wrong place.

Sound familiar?

Common pytest problems we fix

The pytest problems that bring people to us, and what you have once each one is fixed.

ScopeMismatch: 'session' scoped fixture used from 'function' scope

Fixture scopes collide

A cheaper session-scoped fixture being requested by a short-lived function-scoped one, which pytest refuses to wire. The usual cause is a dependency added without reviewing the scope tree.

Scopes aligned, dependencies honest

There is no current event loop

asyncio tests hang or error out

A pytest-asyncio version change that redefined the event_loop fixture, or a test that mixes synchronous and asynchronous clients against the same loop.

asyncio tests running against one loop

database is locked

A shared database blocks parallel runs

SQLite under xdist workers, or a session-scoped Postgres database with no isolation between tests. Transactions-per-test fixtures or a per-worker database both solve it; neither is free.

Parallel runs isolated at the database

pytest-xdist: worker crashed

Parallel workers drop mid-run

A test that writes to a shared file, binds to a fixed port, or mutates a module-level global. The worker crashes, xdist retries, and the first symptom is a flaky test the authors cannot reproduce locally.

Shared resources scoped per worker

fixture 'client' not found

Fixtures are not discovered

A fixture defined in the wrong conftest.py, a missing __init__.py breaking the package graph, or a rootdir that pytest resolved differently from what the project expects.

conftest layout matching the import tree

ImportError while importing test module

Imports fail before tests run

A missing editable install, a sys.path that works locally and not on CI, or a circular import between test helpers and application code.

Imports work from a clean checkout

AttributeError: 'coroutine' object has no attribute

A test forgot to await

An async function being called without await, returning a coroutine object that then gets asserted against as if it were the real result.

Awaits restored, async tests honest

collected 1247 items in 183.4s

The suite is slow out of proportion to its size

Collection overhead from heavy imports in conftest.py, autouse fixtures doing unnecessary setup, and an xdist configuration that is not actually parallelizing the slow tests.

Runtime cut while coverage holds

What we do

pytest help, from one flaky test to a managed suite

Call us for a single failing fixture, or let us take on the whole pytest estate.

Rescue
A red CI pipeline, a fixture that refuses to wire, or an asyncio run that hangs until it times out. We stabilize the suite first and document the cause second.
Fixture and conftest cleanup
Scope review, autouse audit, and a conftest layout the next engineer can read without chasing imports across three directories.
asyncio and async framework work
FastAPI, Starlette, httpx and asyncpg suites wired to one event loop per test, with pytest-asyncio pinned and configured against its latest shape.
Parallelization and speed
pytest-xdist tuned to your real test graph, slow tests isolated, databases scoped per worker, and CI runtime cut without dropping assertions.
Coverage and reporting
pytest-cov configured to be honest about what is exercised, with branch coverage, excluded generated code, and dashboards your team will actually read.
Managed Tech
Ongoing suite upkeep, flake tracking, plugin version pinning and an engineer on hand when a Python or pytest release shifts a default.

How it works

From first message to fixed

  1. Look Free estimate

    Tell us what's going wrong. We look at it, then tell you plainly what's wrong and what it will cost.

  2. Fix

    We fix the cause, not the symptom. If something is down, getting it back up comes first.

  3. Harden

    Backups, monitoring, updates and access, so the same problem can't quietly come back.

  4. Hand back or stay on

    You get a plain-language write-up. Or we stay on for hosting, upkeep and new work.

Often found together

Running pytest alongside something else?

We work on the whole stack, so the problem can be wherever it actually is.

Questions

pytest questions

Something else? Call+1 737 727 7327

Why does our pytest suite pass locally and fail on CI?

Most of the time it is one of four things: a test that depends on wall clock or time zone, a shared database or port that only collides under xdist, a Python version difference between developer machines and CI, or an environment variable that is set in a developer shell and not in the CI container. We reproduce the failure on CI first, then fix the cause instead of adding retries.

How do we handle asyncio tests with pytest?

Pin pytest-asyncio to a specific major, choose either asyncio_mode=auto or strict and stick with it, and avoid creating your own event loops inside tests. If you need a shared loop for a client and a database connection, express that through the fixture tree rather than through global state.

Why are our fixtures leaking state between tests?

A fixture scoped higher than its data lifetime, an autouse fixture that mutates module state, or a yield fixture whose teardown branch never actually runs because of an exception. We walk the scopes, make the mutations explicit, and either lower the scope or add proper teardown.

Can pytest replace unittest for a large Django codebase?

Yes, and pytest-django makes the migration straightforward. Most unittest TestCase classes continue to work, which lets you adopt pytest incrementally rather than rewriting hundreds of files. Fixtures and parametrization are where the real value shows up.

How do we speed up a slow pytest run?

First, measure. pytest --durations tells you which tests are actually slow, and they are usually fewer than the team thinks. From there, options include parallelizing with xdist, caching fixtures sensibly, moving integration tests to a separate stage, and killing autouse fixtures that run on every test for no good reason.

How much does pytest help cost?

The estimate is on us. Share the failing output, the plugin list and the suite size, and a priced scope is in your inbox before any changes happen.

Free estimate

pytest giving you trouble?

Send the failing test, the conftest layout and the plugin versions. A human writes back with a quote, and work only begins once you agree.

  • No charge to find out what it will take
  • If it's urgent, say so

Replies come from a person. By sending, you authorize us to email you back. No marketing.