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#
- Zero configuration. Every test is checked once it is installed; the other SQLAlchemy test plugins in the comparison need a marker, an option or a fixture before a test fails, and some never fail one.
- Few false alarms. In the head-to-head run of 2026-11-10 it flagged 2 of 143 scenarios without a problem; the other working plugins flagged 30 to 35. The corpus is the author's, so read it with that bias.
- More than lazy loads. Repeated statements from
session.get()loops, Core andtext(), deferred and expired column loads, and query budgets that list every statement with the line that sent it. - Missing indexes on PostgreSQL, SQLite, MySQL and MariaDB, with a
CREATE INDEXto add. - Adoption on a large suite: a baseline so only new findings fail, and SARIF for GitHub code scanning.
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:
- Getting started: install, a first failing test and its fix, async code and FastAPI.
- A test failed: now what: read the report, check the finding, fix it or leave it out on purpose.
- CI and GitHub Action: findings as pull request annotations.
- Baseline: adopt it on an existing project; only new findings fail.
- 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.