A simple approval test library utilizing external diff programs such as PyCharm and Visual Studio Code to compare approved and received output.
Approval tests capture the output (a snapshot) of a piece of code and compare it with a previously approved version of the output.
It's most useful in environments where frequent changes are expected or where the output is of a complex nature but can be easily verified by humans, aided for example by a diff-tool or a visual representation of the output.
Once the output has been approved then as long as the output stays the same the test will pass. A test fails if the received output is not identical to the approved version. In that case, the difference between the received and the approved output is reported to the tester.
For outputs that can be represented by text, a report can be as simple as printing the difference to the terminal. Using diff programs with a graphical user interface such as PyCharm or Visual Studio Coder as reporter not only helps to visualize the difference, but they can also be used as approver by applying the changes of the received output to the approved output.
Not all data can or should be represented by text. In many cases an image is the best and most easily verifiable representation. PyCharm and Visual Studio Code can work with images as well.
A picture’s worth a 1000 tests (approvaltests.com).
OS
- Linux/Unix
- MacOS
One of following programs installed:
- PyCharm
- Visual Studio Code
- Meld
- GNU Diffutils (
diff
)
uv add git+https://github.com/GIScience/asyncpg-recorder.git
Verify text:
from pytest_approval import verify, verify_json
def test_verify_string()
assert verify("Hello World!")
def test_verify_dict()
# automatic conversion to JSON
assert verify_json({"msg": "Hello World!"})
Verify binary files such as an image:
from pytest_approval import verify_binary
def test_verify_binary(image):
with open("my_image.jpg", "rb") as file:
buffer = file.read()
assert verify_binary(buffer, extension=".jpg")
It is possible to run auto approve every approval tests:
uv run pytest --auto-approve
This is useful for elimination of approval files which are not in use anymore.
- Make sure tests are green.
- Then remove all approval files.
- Run pytest in auto approval mode.
Approved and received files are stored next to the test file per default.
If you want to save those files in a specific directory instead, please set the approvals-dir
key in your pyproject.toml
:
[tool.pytest-approval]
"approved-dir"="tests/approvals"
This path is relative to your pytest root (usually your pyproject.toml
).
uv sync --all-extras
uv run pytest
This project uses CalVer.
Format is: YYYY.MM.DD
(E.g 2025.9.2
).
In case of releasing twice on one day add a micro number starting with 1:
YYYY.MM.DD.micro
(E.g. 2025.9.2.1
).
To make a new release run ./scripts/release.sh YYYY.MM.DD
Syrupy is a zero-dependency pytest snapshot plugin. It enables developers to write tests which assert immutability of computed results.
Approvaltests is an open source assertion/verification library to aid testing.