https://github.com/apkawa/pytest-image-diff
pytest helps for compare images and regression
https://github.com/apkawa/pytest-image-diff
Last synced: 10 months ago
JSON representation
pytest helps for compare images and regression
- Host: GitHub
- URL: https://github.com/apkawa/pytest-image-diff
- Owner: Apkawa
- License: mit
- Created: 2020-04-01T13:40:38.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-12-31T09:05:01.000Z (over 1 year ago)
- Last Synced: 2025-09-04T16:56:18.863Z (11 months ago)
- Language: Python
- Size: 229 KB
- Stars: 11
- Watchers: 1
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://pypi.python.org/pypi/pytest-image-diff)
[](https://github.com/Apkawa/pytest-image-diff/actions/workflows/ci.yml)
[](https://pytest-image-diff.readthedocs.io/en/latest/?badge=latest)
[](https://codecov.io/gh/Apkawa/pytest-image-diff)
[](https://pypi.python.org/pypi/pytest-image-diff)
[](LICENSE)
# pytest-image-diff
# Installation
```bash
pip install pytest-image-diff
```
or from git
```bash
pip install -e git+https://githib.com/Apkawa/pytest-image-diff.git@master#egg=pytest-image-diff
```
Python>=3.6
# Usage
```python
from typing import Union
from PIL import Image
def test_compare(image_diff):
image: Image or str or bytes = Image.new()
image2: Image or str or bytes = '/path/to/image.jpeg'
image_diff(image, image2)
def test_regression(image_regression):
image: Union[Image, str, bytes] = Image.new()
image_regression(image, threshold=0.5)
```
Also use with assert
```python
import pytest
from typing import Union
from PIL import Image
@pytest.fixture(scope="session")
def image_diff_throw_exception() -> bool:
"""
Set default throw exception. By default - True
"""
return False
def test_compare(image_diff):
image: Image or str or bytes = Image.new()
image2: Image or str or bytes = '/path/to/image.jpeg'
assert image_diff(image, image2)
assert image_diff(image, image2, threshold=0.5)
# Also can check threshold in compare, ie
assert image_diff(image, image2) < 0.5
# For different checks in one test
assert image_diff(image, image2, threshold=0.5, suffix="one")
# Or without fixture image_diff_throw_exception
assert image_diff(image, image2, threshold=0.5, throw_exception=False)
def test_regression(image_regression):
image: Union[Image, str, bytes] = Image.new()
assert image_regression(image, threshold=0.5)
# Also can check threshold in compare, ie
assert image_regression(image) < 0.5
# For different checks in one test
assert image_regression(image, threshold=0.5, suffix="foo")
# Or without fixture image_diff_throw_exception
assert image_regression(image, threshold=0.5, throw_exception=False)
```
First run creates reference images
## pytest-splinter
Fixture `screenshot_regression` enabled if pytest-splinter installed
```python3
import pytest
@pytest.fixture
def admin_browser(request, browser_instance_getter):
"""Admin browser fixture."""
# browser_instance_getter function receives parent fixture -- our admin_browser
return browser_instance_getter(request, admin_browser)
def test_2_browsers(browser, admin_browser, screenshot_regression):
"""Test using 2 browsers at the same time."""
browser.visit('http://google.com')
admin_browser.visit('http://admin.example.com')
screenshot_regression(suffix="browser")
screenshot_regression(admin_browser, suffix="admin browser")
def test_pytest_splinter(browser, screenshot_regression):
# Recommend fix window size for avoid regression
browser.driver.set_window_size(1280, 1024)
browser.visit('http://google.com')
screenshot_regression(suffix="main")
# ... some interaction
browser.click()
screenshot_regression(suffix="success")
# you can use xpath expression for part of page
screenshot_regression(xpath="//h1")
```