pytest-queryguard#

A pytest plugin that fails tests with N+1 queries and enforces query budgets for SQLAlchemy 2, in sync, async and FastAPI code. On PostgreSQL 16 and newer, SQLite, MySQL and MariaDB it can also fail tests whose queries have no index to use.

ORM code that looks correct can send one query per row instead of one query per page. The tests pass, because a test database holds a handful of rows. In production the same endpoint sends hundreds of queries. pytest-queryguard watches the SQL each test sends, and when it sees the pattern it fails the test and points to the line that caused it.

queryguard: N+1 detected

  Order.customer was lazy-loaded 50 times
  first at app/orders.py:10 in list_orders
  parents loaded at app/orders.py:9 in list_orders
  kind: relationship-load  label: Order.customer
  docs: https://maciejziel.github.io/pytest-queryguard/how-detection-works.html#relationship-load
  sql: SELECT customer.id, customer.name FROM customer WHERE customer.id = ?
  fix: load it with the parent query, e.g.
       select(Order).options(joinedload(Order.customer))
pip install pytest-queryguard

It is on by default: there is nothing to import or configure. These pages describe 1.1.0, see Getting started. If pytest does not know an option, check the changelog for the release that added it.

What is different#

What it does not do (ORMs other than SQLAlchemy and Django) and where other tools are ahead: Comparison with other tools and Limitations. How often it is right, on a corpus of 283 scenarios: Accuracy. Coming from another tool: nplusone, queryspy.

Where to go#

The path most projects take, one page each:

  1. Getting started: install, a first failing test and its fix, async code and FastAPI.
  2. A test failed: now what: read the report, check the finding, fix it or leave it out on purpose.
  3. CI and GitHub Action: findings as pull request annotations.
  4. Baseline: adopt it on an existing project; only new findings fail.
  5. Index check and index advisor: queries no index can serve, on PostgreSQL, SQLite and MySQL and MariaDB.

Reference: How detection works, Configuration, Budgets and timing, Ignoring findings. Before you adopt it: FAQ and Stability and versioning.

The source and the issue tracker are on GitHub.

Further reading: My N+1 test plugin failed 551 tests on someone else's project, the launch article on dev.to: what a run on another project's suite found and why nearly all of it was noise.

Edit this page on GitHub