Package: pytest-image-snapshot
pytest-image-snapshotA pytest plugin for image snapshot management and comparison.FeaturesImage 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-updateflag to update existing snapshots or create new ones, accommodating visual changes in your project.Threshold-Based Comparison: Utilizes thethresholdargument for enhanced image comparison with thepixelmatchlibrary, enabling anti-aliasing pixel detection.RequirementsPillowpixelmatch (optional)InstallationYou can install "pytest-image-snapshot" via pip fromPyPI:$ pip install pytest-image-snapshotOptional Dependencypytest-image-snapshotoffers enhanced functionality with the optionalpixelmatchpackage, suitable for advanced image comparison scenarios. To installpytest-image-snapshotalong with this optional feature, use the following command:$pipinstallpytest-image-snapshot[pixelmatch]Pytest Image Snapshot Usage ExampleTheimage_snapshotfixture 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.UsageHere's a example of how to utilize theimage_snapshotfixture:fromPILimportImagedeftest_image(image_snapshot):# Create a new white image of 100x100 pixelsimage=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 createdimage_snapshot(image,"test_snapshots/test.png")OptionalthresholdArgument inimage_snapshotTheimage_snapshotfunction includes an optionalthresholdargument. When set, and if the image does not match the snapshot, thepixelmatchlibrary is used for a detailed comparison with anti-aliasing pixel detection.Default Threshold:Ifthresholdis set toTrue, a default threshold value is utilized.Custom Threshold:Ifthresholdis a numeric value, it is passed to thepixelmatchlibrary 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:Theimage_snapshotfixture 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 (-vor--verbose)The verbose mode enhances the output detail forimage_snapshottests:-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-updateflag 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-updateExampleVisual regression test forDjangoapplication home page withplaywright:fromPILimportImagefromioimportBytesIOdeftest_homepage(live_server,page:Page,image_snapshot):page.goto(f"{live_server}")# convert screenshot to imagescreenshot=Image.open(BytesIO(page.screenshot()))image_snapshot(screenshot,"test_snapshots/homepage.png",threshold=True)ContributingContributions are very welcome. Tests can be run withtox, please ensure the coverage
at least stays the same before you submit a pull request.LicenseDistributed under the terms of theMITlicense,
"pytest-image-snapshot" is free and open source softwareIssuesIf you encounter any problems, pleasefile an
issuealong
with a detailed description.Thispytestplugin was generated
withCookiecutteralong with@hackebrot'scookiecutter-pytest-plugintemplate.