pytest-sqlguard 🔍🛡️
pytest-sqlguard is a pytest plugin that helps you safeguard your SQLAlchemy queries against unintended changes.
It records all database queries executed during tests and compares them against previously recorded "reference" queries,
alerting you to any differences or regressions that may impact SQL functionality or performance.
✅ Easily detect unintended database query changes, regressions
✅ Prevent accidental database performance degradation
✅ Simple integration with SQLAlchemy & pytest
Installation 📦
uv add pytest-sqlguard
Usage 🚀
Configure your fixture as you like and then it can be as simple as:
def test_your_database_logic(sqlguard, session):
with sqlguard(session):
test_client.get("/users/random_id")
When you run the test for the first time, pytest-sqlguard records all queries executed during the test and stores them as "reference data".
On subsequent test runs, it compares current queries against the reference, alerting you if any differences are found.
The stored data is in a yaml file named as your test file but with the .queries.yaml extension:
test_your_database_logic:
queries:
- statement: |-
SELECT ...
FROM public.users
WHERE public.users.id = %(pk_1)s::UUID
Command Line Options ⚙️
The extension provides two configurable behaviors (that defaults to False for both of them)
- Fail Missing: Fail if the queries are missing in the reference file.
- Overwrite: If set to True, it will overwrite the reference query
You can add option flag to pytest to control those behaviors:
# In your conftest.py
def pytest_addoption(parser):
sqlguard_group = parser.getgroup("sqlguard", "Options for SQLGuard")
sqlguard_group.addoption(
"--sqlguard-fail-missing",
dest="sqlguard_fail_missing",
default=False,
action="store_true",
help="SQLGuard: Fail if queries are missing in the stored reference file.",
)
sqlguard_group.addoption(
"--sqlguard-overwrite",
dest="sqlguard_overwrite",
default=False,
action="store_true",
help="SQLGuard: Overwrite the stored reference file.",
)
For example, to trigger an error if the stored reference queries are missing, run:
pytest --sqlguard-fail-missing
To update reference queries after intended SQL changes, use:
pytest --sqlguard-overwrite
Configuring the Fixture in Tests ⚗️
You need to define your fixture in your conftest.py file.
Here is an example of a working fixture:
@pytest.fixture(scope="function")
def sqlguard(request, tmp_path_factory, session):
# We use the previously defined pytest cli arguments
fail_on_missing_reference_data = request.config.option.sqlguard_fail_missing
overwrite_reference_data = request.config.option.sqlguard_overwrite
from pytest_sqlguard.sqlguard import sqlguard as sqlguard_function
yield sqlguard_function(
request,
tmp_path_factory,
fail_on_missing_reference_data,
overwrite_reference_data,
)
You can also use the assert representation we created to show the differences:
# In conftest.py
from pytest_sqlguard.sqlguard import assertrepr_compare
def pytest_assertrepr_compare(config, op, left, right):
return assertrepr_compare(config, op, left, right)
Releasing a new version 🚚
# in your local shell
bumpver update ## Updates the version number, commits the change, tags the commit
git push # The deployment is made on tag push
Contributing 🤝
Issues, feature requests, and contributions are warmly welcomed! Feel free to open a pull request or submit an issue.
Credits 🙏
This software has been created inside Paylead. Thanks to all the people that made it possible including but not limited to:
License 📜
pytest-sqlguard is distributed under the MIT license. See LICENSE for details.
Release files for pytest-sqlguard 2025.606.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pytest_sqlguard-2025.606.0.tar.gz | 19.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytest_sqlguard-2025.606.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 31.0 kB
Release files / pytest_sqlguard-2025.606.0.tar.gz
| Download URL | pytest_sqlguard-2025.606.0.tar.gz |
|---|---|
| Size | 19.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
01fed7317efe7fd45030f1e13d339e90ffb0d82b6f375d985422e65f52f3fd13
|
|
BLAKE2b-256 checksum How to use checksums |
3ee314bddd63bf6f667598e1ebbe20b6532d0d5eade5a2201bfa46fbf3113ba7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 6, 2025.
Transparency logRelease files / pytest_sqlguard-2025.606.0-py3-none-any.whl
| Download URL | pytest_sqlguard-2025.606.0-py3-none-any.whl |
|---|---|
| Size | 11.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d702f68f83d214539896baf5835453986377cf6bfa110c42f3bdfb936e59d2e8
|
|
BLAKE2b-256 checksum How to use checksums |
e0927fb48bc644b34b71136ecc5579a6b125138c0b8619ad2d5d1c1f9d4cbae8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 6, 2025.
Transparency log