Skip to main content

pytest approval

Build Status Sonarcloud Status PyPI - Version LICENSE status: active

A simple approval test library utilizing external diff programs such as Meld, PyCharm and Visual Studio Code to compare approved and received output.

About

Approval tests capture the output (a snapshot) of a piece of code and compare it with a previously approved version of the output (the expected result).

It's most useful in environments where frequent changes are common 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 (think of an image).

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 Meld, PyCharm or Visual Studio Code 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).

Requirements

OS

  • Linux/Unix
  • macOS

One of following programs installed:

  • PyCharm
  • Visual Studio Code
  • Meld
  • GNU Diffutils (diff)

Installation

uv add pytest-approval

# Including image support
uv add --optional image pytest-approval

# Including plotly support
uv add --optional plotly pytest-approval

Usage

Verify Text

from pytest_approval import verify, verify_json


def test_verify_as_string():
    assert verify("Hello World!")


def test_verify_as_json():
    # automatic conversion to JSON
    assert verify_json({"msg": "Hello World!"})
    # works with string as well
    assert verify_json('{"msg": "Hello World!"}')

Verify Images

To report images visually make sure PyCharm or Visual Studio Code is installed.

from PIL import Image
from pytest_approval import verify_image, verify_image_pillow


def test_verify_image(image):
    image = Image.open("my_image.jpg")
    assert verify_image(image, extension=".jpg", content_only=True)


def test_verify_image_pillow(image):
    image = Image.open("my_image.jpg")
    assert verify_image_pillow(image, extension=".jpg")

Verify Plotly Figures

For comparison the JSON representation of a Plotly figure is used and for reporting the image representation.

from pytest_approval import verify_plotly
import plotly.graph_objects as go

FIGURE = go.Figure(
    data=go.Contour(
        z=[
            [10, 10.625, 12.5, 15.625, 20],
            [5.625, 6.25, 8.125, 11.25, 15.625],
            [2.5, 3.125, 5.0, 8.125, 12.5],
            [0.625, 1.25, 3.125, 6.25, 10.625],
            [0, 0.625, 2.5, 5.625, 10],
        ]
    )
)


def test_verify_plotly():
    assert verify_plotly(FIGURE)

Force Reporting

During development its sometimes helpful to report even though both are equal:

from pytest_approval import verify


def test_verify_string():
    assert verify("Hello World!", report_always=True)

Auto Approval

It is possible to automatically approve every tests:

uv run pytest --auto-approve

This is useful for elimination of approval files which are not in use anymore.

  1. Make sure tests are green.
  2. Then remove all approval files.
  3. Run pytest in auto approval mode.

Configuration

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]
"approvals-dir"="tests/approvals"  

The path is relative to pytest root (usually pyproject.toml).

Development

uv sync --all-extras
uv run prek install  # pre-commit hooks
uv run pytest
uv run pytest --markdown-docs -m markdown-docs README.md

Release

This project uses SemVer.

To make a new release run ./scripts/release.sh <version>.

Alternatives

  • Syrupy is a zero-dependency pytest snapshot plugin. It enables developers to write tests which assert immutability of computed results.
  • ApprovalTests.Python is an open source assertion/verification library to aid testing.

Comparison with ApprovalTests.Python

ApprovalTests.Python and ApprovalTests in general are the main inspiration for this library. The goal of this library is to provide a much smaller, simpler and maintainable code base while at the same time providing a simpler interface.

In contrast to ApprovalTests.Python this library features:

Comparison with Syrupy

  • Approval in syrupy happens by passing a command line argument --snapshot-update to PyTest.
  • Syrupy has not built-in diff reporter for images (See issues #886 and #566).

Metadata

Release files for pytest-approval 0.18.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-approval 0.18.1
File Size Uploaded
pytest_approval-0.18.1.tar.gz 28.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-approval 0.18.1
File Interpreter ABI Platform
pytest_approval-0.18.1-py3-none-any.whl Python 3 none any Details

Total release size: 60.1 kB

Release files / pytest_approval-0.18.1.tar.gz

Download URL pytest_approval-0.18.1.tar.gz
Size 28.9 kB
Tags Source
SHA-256 checksum
How to use checksums
040afe250fb1aec9890720cd4bc2ea9b62f6bbe1230af46a2b76839dcabfc801
BLAKE2b-256 checksum
How to use checksums
3a7d2573cc8867cba0b5593ff1bb43736a6a0639f934c537653be6fe8e7d8a3c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.13 {"installer":{"name":"uv","version":"0.12.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / pytest_approval-0.18.1-py3-none-any.whl

Download URL pytest_approval-0.18.1-py3-none-any.whl
Size 31.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1f479844296e2578349a6e85508ec777b4109c4029aecf7673049bdc405f45a6
BLAKE2b-256 checksum
How to use checksums
da3920a33099d9e0647604b8f033eb45e80d635c0fca37c4c5fa6ff1c803ebf3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.13 {"installer":{"name":"uv","version":"0.12.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.18.1 This release

2 release files

0.17.0

2 release files

0.16.1

2 release files

0.16.0

2 release files

0.15.1

2 release files

0.14.0

2 release files

0.12.0

2 release files

0.11.0

2 release files

0.10.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page