Skip to main content

A pytest plugin for image snapshot management and comparison.

Project description

pytest-image-snapshot

PyPI version

Python versions

A pytest plugin for image snapshot management and comparison.


Features

  • Image Comparison: Automatically compares a test-generated image with a pre-stored snapshot, identifying any visual discrepancies.
  • Snapshot Creation: If a reference snapshot doesn't exist, the plugin will create it during the test run, making initial setup effortless.
  • Verbose Mode Display: Capable of displaying the difference image for quick visual feedback in case of mismatches when running tests with -v.
  • Snapshot Update Option: Includes a --image-snapshot-update flag to update existing snapshots or create new ones, accommodating visual changes in your project.
  • Threshold-Based Comparison: Utilizes the threshold argument for enhanced image comparison with the pixelmatch library, enabling anti-aliasing pixel detection.

Requirements

  • Pillow
  • pixelmatch (optional)

Installation

You can install "pytest-image-snapshot" via pip from PyPI:

$ pip install pytest-image-snapshot

Optional Dependency

pytest-image-snapshot offers enhanced functionality with the optional pixelmatch package, suitable for advanced image comparison scenarios. To install pytest-image-snapshot along with this optional feature, use the following command:

$ pip install pytest-image-snapshot[pixelmatch]

Pytest Image Snapshot Usage Example

The image_snapshot fixture is designed for visual regression testing in pytest. It compares a generated image in your tests with a stored reference image (snapshot). If the snapshot doesn't exist, it will be automatically created. This makes the fixture ideal for both creating initial snapshots and for ongoing comparison in visual tests.

Usage

Here's a example of how to utilize the image_snapshot fixture:

from PIL import Image

def test_image(image_snapshot):
    # Create a new white image of 100x100 pixels
    image = Image.new('RGB', (100, 100), 'white')
    # Compare it to the snapshot stored in test_snapshots/test.png
    # If test_snapshots/test.png does not exist, it will be created
    image_snapshot(image, "test_snapshots/test.png")

Optional threshold Argument in image_snapshot

The image_snapshot function includes an optional threshold argument. When set, and if the image does not match the snapshot, the pixelmatch library is used for a detailed comparison with anti-aliasing pixel detection.

  • Default Threshold: If threshold is set to True, a default threshold value is utilized.
  • Custom Threshold: If threshold is a numeric value, it is passed to the pixelmatch library to specify the tolerance level for image comparison.
image_snapshot(image, "test_snapshots/test.png", True)
image_snapshot(image, "test_snapshots/test.png", 0.2)

⚠️ Warning:

The image_snapshot fixture does not automatically create directories for storing image snapshots. Ensure that the necessary directories (e.g., test_snapshots/) are created in your project structure before running tests.

Verbose Mode (-v or --verbose)

The verbose mode enhances the output detail for image_snapshot tests:

  • -v: Displays the 'diff' image when there's a mismatch.
  • -vv: Shows all three images - 'diff', 'original', and 'current' for a comprehensive comparison.

This feature assists in quickly identifying and analyzing visual differences during test failures.

Save actual image and diff image (--image-snapshot-save-diff)

Use the --image-snapshot-save-diff flag to save the actual image and the diff image when there's a mismatch. This is particularly useful for debugging in a CI environment.

pytest --image-snapshot-save-diff

Updating Snapshots (--image-snapshot-update)

Use the --image-snapshot-update flag to update or create new reference snapshots. This is useful for incorporating intentional visual changes into your tests, ensuring that your snapshots always reflect the current expected state.

pytest --image-snapshot-update

Failing when snapshots are missing (--image-snapshot-fail-if-missing)

Use the --image-snapshot-fail-if-missing flag to fail the test when the snapshot is missing. This is particularly useful in CI check to ensure that all snapshots are present and up-to-date.

pytest --image-snapshot-fail-if-missing

Example

Visual regression test for Django application home page with playwright:

from PIL import Image
from io import BytesIO

def test_homepage(live_server, page: Page, image_snapshot):
    page.goto(f"{live_server}")
    # convert screenshot to image
    screenshot = Image.open(BytesIO(page.screenshot()))
    image_snapshot(screenshot, "test_snapshots/homepage.png", threshold=True)

Contributing

Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the MIT license, "pytest-image-snapshot" is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.


This pytest plugin was generated with Cookiecutter along with @hackebrot's cookiecutter-pytest-plugin template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_image_snapshot-0.5.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_image_snapshot-0.5.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest_image_snapshot-0.5.0.tar.gz.

File metadata

  • Download URL: pytest_image_snapshot-0.5.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pytest_image_snapshot-0.5.0.tar.gz
Algorithm Hash digest
SHA256 cd6d73642d4f42514bdb6284642c99be4059122cb2b4234424234d68ed91aacd
MD5 faf6d91e2bd46263b07330a46a4a4a03
BLAKE2b-256 f06640e7ea2678fc272c423a1d724aa0682de69e8f2077c122b592b8b15240d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_image_snapshot-0.5.0.tar.gz:

Publisher: release.yml on bmihelac/pytest-image-snapshot

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_image_snapshot-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_image_snapshot-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2ac5b72998e28203e9a3d23853c5d94531bc86667a2351ba97b02c2b93370d7d
MD5 6d973a1f16905e0a1a7aad73cbc4a63d
BLAKE2b-256 974477da3c68e17ab2aac15062de20ae6989c598bfe0ffa83ba6e3d8c9029229

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_image_snapshot-0.5.0-py3-none-any.whl:

Publisher: release.yml on bmihelac/pytest-image-snapshot

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page