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.

Requirements

  • Pillow

Installation

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

$ pip install pytest-image-snapshot

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")

⚠️ 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.

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

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")

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.1.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

pytest_image_snapshot-0.1.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file pytest-image-snapshot-0.1.1.tar.gz.

File metadata

  • Download URL: pytest-image-snapshot-0.1.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for pytest-image-snapshot-0.1.1.tar.gz
Algorithm Hash digest
SHA256 148818b522968469d2b49a9b1bebb2b5f7bf7ae5d319d824c608b426a47f44a4
MD5 0d4d626cc474610fc633b2c8cc7f6b9c
BLAKE2b-256 69459020ff5ee8263034452aa8dfbe5717f5a9d2dfb5d07ed24035676291a3b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytest_image_snapshot-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a38e1a0a3fa1ad7c557495f749a7354d6a507568dff5b1a0ff135b285b40fd7f
MD5 7b28d58921ed6744e75b88eadfd8dac6
BLAKE2b-256 1bfcc1a9b098ac7e5a07fdcf4b8e0a33c6b052f2156f015fe0cd81c96f7fd256

See more details on using hashes here.

Supported by

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