pytest-queryguard 1.0.0 is out.
It is a pytest plugin that fails a test when the code it runs sends one
query per row (N+1), and enforces query budgets per test. It works with
SQLAlchemy 2 (sync and async sessions, FastAPI, Flask-SQLAlchemy, SQLModel,
Litestar) and needs no code changes: install it and run your tests. The
failure names the line, the SQL and the fix, for example a joinedload on the
parent query.
What it checks by default: a relationship lazy-loaded for 2 or more parents,
the same SELECT run 5 or more times (ORM, Core or text()), columns reloaded
one object at a time, and cartesian joinedloads (as a warning). Opt-in:
row-by-row writes and missing indexes (PostgreSQL 16+, SQLite, MySQL,
MariaDB). An N+1 that runs only in test code (conftest.py, factories,
helpers under tests/) is counted at the end of the run instead of failing
the test. A baseline file lets an existing suite adopt it without fixing
everything first, and a GitHub Action uploads findings to code scanning and
compares query counts with the base branch on a pull request.
What 1.0 means: the options, markers, finding kinds, report files and the
Action change incompatibly only in a major release, and deprecations last
at least two minor releases. Django ORM support (next to pytest-django), the
static check and the MCP server are still experimental and may change in a
minor release.
Accuracy and overhead are measured and published, misses included: on 278
labelled scenarios the defaults flag 117 of 135 with a problem and 2 of 143
without one. The corpus is written by me, so read those numbers with that
in mind; the comparison page has the method and the other tools' results.
Python 3.10+, pytest 8+, SQLAlchemy 2.0.13+. MIT licence.
Install: pip install pytest-queryguard
Docs: https://maciejziel.github.io/pytest-queryguard/
Changes:
https://github.com/MaciejZiel/pytest-queryguard/blob/main/CHANGELOG.md
Source: https://github.com/MaciejZiel/pytest-queryguard
Maciej Zieliński
[Python-announce] ANN: pytest-queryguard 1.0.0 - fail pytest tests on N+1 queries
- From
- maciej zielinski <ziel…@gmail.com>
- Reply-To
- pyth…@python.org
- Message-ID
- <CAOh…@mail.gmail.com>
- Date
- Newsgroups
- comp.lang.python.announce
Moderated This group is moderated.