Migrating from queryspy#
queryspy and pytest-queryguard
both find N+1 queries and enforce query budgets for SQLAlchemy 2 in pytest.
This page maps each queryspy setting to its pytest-queryguard equivalent. It
was written against queryspy 0.5.0 (read in its installed package on
2026-10-13); on 2026-11-08 the options, markers and helpers below were checked
again in the installed queryspy 0.6.0, the current release, and are the same
(0.6.0 adds --queryspy-report-format). The pytest-queryguard column is
current for 1.0.0. It is written by the author of
pytest-queryguard; Comparison has the measured differences,
including the ones where queryspy is ahead.
Before you move: queryspy also runs in your application, as ASGI and WSGI middleware with a request panel. pytest-queryguard has had an in-application mode of its own since 0.5.0, with ASGI and WSGI middleware and an optional panel; the two were not compared on running apps. If you keep the queryspy middleware, running both plugins in one pytest session was not tested (both read SQLAlchemy events, and both register a marker of their own, so the names do not clash).
The main difference in defaults#
queryspy reports N+1 findings and fails no test on them unless you pass
--queryspy-strict or set queryspy_fail_on (its budgets fail either way).
pytest-queryguard fails a test on any finding with no configuration. To keep the queryspy behaviour while you
migrate, start in warn mode and switch to fail once the suite is clean or
baselined:
[tool.pytest.ini_options]
queryguard_mode = "warn"
Find and replace#
What to change in the source, line by line; the sections below explain each difference.
| Find | Replace with |
|---|---|
--queryspy-strict |
Delete it: failing is the default |
queryspy_fail_on = lazy_load, column_load |
queryguard_fail_on = ["relationship-load", "column-load"] |
queryspy_budget = 20 |
queryguard_max_queries = 20 |
--queryspy-report=queryspy.sarif |
--queryguard-sarif=queryguard.sarif |
--queryspy-report=queryspy.json |
--queryguard-json=queryguard.json (another format) |
--queryspy-baseline=PATH |
--queryguard-baseline=PATH (record it again: another format) |
--queryspy-baseline-update |
--queryguard-update-baseline |
@pytest.mark.queryspy(max_queries=3) |
@pytest.mark.max_queries(3) |
@pytest.mark.queryspy(allow_n_plus_one=True) |
@pytest.mark.queryguard_ignore |
from queryspy import assert_max_queries |
from pytest_queryguard import max_queries |
with assert_max_queries(2): |
with max_queries(2): |
from queryspy import no_n_plus_one |
from pytest_queryguard import record_queries |
with no_n_plus_one(): |
with record_queries() as log:, then log.assert_no_nplusone() after the block |
from queryspy import record |
from pytest_queryguard import record_queries |
with record() as recorder: |
with record_queries() as log: |
with queryspy.ignore(): |
with pytest_queryguard.ignore("*"): |
queryspy.NPlusOneError in pytest.raises |
pytest_queryguard.NPlusOneDetected |
queryspy.QueryCountError in pytest.raises |
pytest_queryguard.QueryBudgetExceeded |
Loops in the test itself#
queryspy reports an N+1 wherever it runs in the test: a loop written in the
test function fails it with --queryspy-strict (checked with queryspy 0.6.0
on 2026-11-08). pytest-queryguard, by default, leaves out the N+1s of test
code: the test function, conftest.py, factories and helpers under tests/.
They are counted in a queryguard: test code section at the end of the run,
which names the tests. A loop in the application that a test calls is
reported as usual. So after the move, a suite whose queryspy findings were
in test code reports fewer findings; to see them, or to keep failing on them:
[tool.pytest.ini_options]
queryguard_test_code = "warn" # or "fail"
See Test code.
Configuration#
| queryspy 0.6.0 | pytest-queryguard |
|---|---|
--queryspy-strict |
Nothing: the default queryguard_mode = fail fails on every finding kind |
queryspy_fail_on = none (the default: report only) |
queryguard_mode = warn |
queryspy_fail_on = n_plus_one (every kind) |
The default; queryguard_fail_on empty |
queryspy_fail_on = lazy_load, column_load |
queryguard_fail_on = ["relationship-load", "column-load"]; the other kinds become warnings |
queryspy_budget = 20 (every test) |
queryguard_max_queries = 20: every test without a max_queries marker or a budget file entry gets it (Budgets) |
queryspy_capture_stacks = false |
No equivalent: the call site is always recorded |
--queryspy-report=queryspy.sarif (or --queryspy-report-format=sarif) |
--queryguard-sarif=queryguard.sarif (SARIF) |
--queryspy-report=queryspy.json (JSON) |
--queryguard-json=queryguard.json: each test's statement count, database time and findings (CI); the format differs |
--queryspy-baseline=PATH |
--queryguard-baseline=PATH, or queryguard_baseline in the configuration |
--queryspy-baseline-update |
--queryguard-update-baseline |
| The queryspy GitHub Action | The action in this repository |
Finding kinds and thresholds#
| queryspy | pytest-queryguard | Default threshold here |
|---|---|---|
lazy_load |
relationship-load |
2 parent objects (queryguard_relationship_threshold) |
column_load |
column-load |
5 instances (queryguard_column_load_threshold) |
repeated_statement |
repeated-statement |
5 runs (queryguard_repeat_threshold) |
| not reported | write-loop, opt-in |
off (queryguard_write_loop_threshold) |
| not reported | missing-index, opt-in |
off (queryguard_index_check) |
queryspy uses one threshold of 2 for every kind. pytest-queryguard reports a
repeated statement and column reloads from 5, so a loop of 2 to 4 statements
that queryspy reported passes here. To get closer to queryspy, set
queryguard_small_repeat_threshold = 2, which reports 2 to 4 runs from one
source line with different parameters, or lower the thresholds themselves:
[tool.pytest.ini_options]
queryguard_small_repeat_threshold = 2
queryguard_column_load_threshold = 2
Markers and helpers#
| queryspy | pytest-queryguard |
|---|---|
@pytest.mark.queryspy(max_queries=3) |
@pytest.mark.max_queries(3) |
@pytest.mark.queryspy(allow_n_plus_one=True) |
@pytest.mark.queryguard_ignore (alias allow_nplusone), or with patterns to allow only some findings: @pytest.mark.queryguard_ignore("Order.customer") |
@pytest.mark.queryspy(threshold=3) |
No per-test threshold marker: set the ini option, or use record_queries() with assert_no_nplusone(relationship_threshold=3, repeat_threshold=3) |
@pytest.mark.queryspy(fail_on=...) |
No per-test equivalent; queryguard_fail_on applies to the suite |
with assert_max_queries(2): |
with max_queries(2): (raises QueryBudgetExceeded, an AssertionError) |
with assert_num_queries(2): (exact) |
with queryguard.num_queries(2): with the queryguard fixture (only the test's own thread counts), or with record_queries() as log: and then assert len(log) == 2 |
with no_n_plus_one(): |
with record_queries() as log: and then log.assert_no_nplusone() |
with record() as recorder: |
with record_queries() as log: (Reading the queries) |
with queryspy.ignore(): in application code |
with pytest_queryguard.ignore("*"):, or name the kinds; threads started inside the block are not covered (the ignore() block) |
QuerySpyMiddleware (ASGI, WSGI), the panel |
QueryGuardASGI, QueryGuardWSGI with panel=True (in-application mode; not compared with queryspy's) |
The helpers, side by side. They run against the fixed fetch_customers, the
one in app/customers_fixed.py of the first test,
which loads the three customers with one IN query; the app.customers
module of that page is the N+1 version, and both tests below fail against it:
from pytest_queryguard import max_queries, record_queries
from app.customers_fixed import fetch_customers
def test_fetch_customers_is_one_query(session):
with record_queries() as log:
fetch_customers(session, [1, 2, 3])
assert len(log) == 1 # queryspy: assert_num_queries(1)
log.assert_no_nplusone(repeat_threshold=2) # queryspy: no_n_plus_one()
def test_fetch_customers_budget(session):
with max_queries(2): # queryspy: assert_max_queries(2)
fetch_customers(session, [1, 2, 3])
assert_no_nplusone() does not read the ini thresholds; pass them as
arguments. Its defaults are 2 for relationships, 5 for repeats and 5 for
column loads; the thresholds for small repeats and write loops are off unless
passed (see Reading the queries).
A budget for every test#
queryspy_budget becomes queryguard_max_queries; a test with a
max_queries marker, or with an entry in the budget file, keeps its own:
[tool.pytest.ini_options]
queryguard_max_queries = 20
To record each test's current count instead of one number for all, use the budget file.
The baseline#
The two baseline files have different formats, and an entry is not the same
identity: pytest-queryguard keys on kind, label, file and function, with paths
relative to pytest's rootdir. Record a new one with
pytest --queryguard-baseline=queryguard-baseline.json --queryguard-update-baseline
on a full run (Baseline). Like queryspy, pytest-queryguard
(since 1.1.0) leaves AS column aliases out of baseline labels, so one
baseline works on SQLAlchemy 2.0 and 2.1; it also leaves out schema
qualifiers, identifier quotes and the placeholder style, so the same file
matches on SQLite and PostgreSQL
(Baseline).
Steps#
pip uninstall queryspy(unless you keep its middleware), thenpip install pytest-queryguard.- Translate the configuration with the tables above; start with
queryguard_mode = "warn"if you ran queryspy without--queryspy-strict. - Replace
@pytest.mark.queryspy(...)markers and thequeryspyhelpers. - Run the full suite, read the findings (A test failed: now what), and record a baseline for those you keep.
- Switch to
queryguard_mode = "fail"(or remove the line) and update the CI job (CI and GitHub Action).