-
Notifications
You must be signed in to change notification settings - Fork 23
add diff_pixels, diff_ratio for acceptence, similar as playwright node.js version #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hgye
wants to merge
16
commits into
kumaraditya303:main
Choose a base branch
from
hgye:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d4edb21
snapshots_tests_failures dir creation with Actual/Diff/Expected snaps…
c0f5eaf
new snapshot creation when file does not exist with pytest.fail(of_co…
e382315
test_results_dir removal before test
b82b233
linted and added comments
08b6179
added ci tests and fail on --update-snapshots
901da0f
updated ci.yml
5a48c67
updated ci.yml
7416ecc
added publish-to-test-pypi.yml
31898e8
Added publish_to_pypi.yml, redacted Readme, updated config.py
ad57c7c
redacted Readme, updated config.py
aad0559
feat: add fail_fast option
8e2302b
fail_fast typo fix
0651136
fix: test_compare_with_fail_fast
05b2d5b
chore: tag version tweak
0f8769e
fix, chore: threshold default value, yml renaming, Readme
a3f67e9
add diff_pixels, diff_ratio for acceptence, similar as playwright nod…
hgye File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,20 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
push | ||
|
||
env: | ||
PLAYWRIGHT_BROWSERS_PATH: 0 | ||
|
||
jobs: | ||
infra: | ||
name: Lint | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: 3.9 | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements.txt | ||
pip install -e . | ||
python -m playwright install-deps | ||
python -m playwright install | ||
- name: Lint | ||
uses: pre-commit/[email protected] | ||
|
||
test: | ||
name: Test | ||
timeout-minutes: 30 | ||
strategy: | ||
fail-fast: true | ||
matrix: | ||
os: [ubuntu-latest, windows-latest, macos-latest] | ||
python-version: [3.7, 3.8, 3.9] | ||
python-version: [3.8, 3.9, 3.10.4] | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- uses: actions/checkout@v2 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
name: Publish Python 🐍 distributions 📦 to PyPI and TestPyPI | ||
|
||
on: | ||
push | ||
|
||
jobs: | ||
build-n-publish: | ||
name: Build and publish Python 🐍 distributions 📦 to PyPI and TestPyPI | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@master | ||
- name: Set up Python 3.10.4 | ||
uses: actions/setup-python@v1 | ||
with: | ||
python-version: 3.10.4 | ||
- name: Install pypa/build | ||
run: >- | ||
python -m | ||
pip install | ||
build | ||
--user | ||
- name: Build a binary wheel and a source tarball | ||
run: >- | ||
python -m | ||
build | ||
--sdist | ||
--wheel | ||
--outdir dist/ | ||
# - name: Publish distribution 📦 to Test PyPI | ||
# uses: pypa/gh-action-pypi-publish@release/v1 | ||
# with: | ||
# user: __token__ | ||
# password: ${{ secrets.TEST_PYPI_API_TOKEN }} | ||
# repository_url: https://test.pypi.org/legacy/ | ||
- name: Publish distribution 📦 to PyPI | ||
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags') | ||
uses: pypa/gh-action-pypi-publish@release/v1 | ||
with: | ||
user: __token__ | ||
password: ${{ secrets.PYPI_API_TOKEN }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import sys | ||
import os | ||
import shutil | ||
import math | ||
from io import BytesIO | ||
from pathlib import Path | ||
from typing import Any, Callable | ||
import pytest | ||
from PIL import Image | ||
from pixelmatch.contrib.PIL import pixelmatch | ||
|
||
|
||
@pytest.fixture | ||
def assert_snapshot(pytestconfig: Any, request: Any, browser_name: str) -> Callable: | ||
test_name = f"{str(Path(request.node.name))}[{str(sys.platform)}]" | ||
test_dir = str(Path(request.node.name)).split('[', 1)[0] | ||
|
||
def compare(img: bytes, *, diff_pixels: int = 0, diff_ratio: float = 0.0, | ||
threshold: float = 0.1, name=f'{test_name}.png', fail_fast=False) -> None: | ||
update_snapshot = pytestconfig.getoption("--update-snapshots") | ||
test_file_name = str(os.path.basename(Path(request.node.fspath))).strip('.py') | ||
filepath = ( | ||
Path(request.node.fspath).parent.resolve() | ||
/ 'snapshots' | ||
/ test_file_name | ||
/ test_dir | ||
) | ||
filepath.mkdir(parents=True, exist_ok=True) | ||
file = filepath / name | ||
# Create a dir where all snapshot test failures will go | ||
results_dir_name = (Path(request.node.fspath).parent.resolve() | ||
/ "snapshot_tests_failures") | ||
test_results_dir = (results_dir_name | ||
/ test_file_name / test_name) | ||
# Remove a single test's past run dir with actual, diff and expected images | ||
if test_results_dir.exists(): | ||
shutil.rmtree(test_results_dir) | ||
if update_snapshot: | ||
file.write_bytes(img) | ||
pytest.fail("--> Snapshots updated. Please review images") | ||
if not file.exists(): | ||
file.write_bytes(img) | ||
# pytest.fail( | ||
pytest.fail("--> New snapshot(s) created. Please review images") | ||
img_a = Image.open(BytesIO(img)) | ||
img_b = Image.open(file) | ||
img_diff = Image.new("RGBA", img_a.size) | ||
mismatch = pixelmatch(img_a, img_b, img_diff, threshold=threshold, fail_fast=fail_fast) | ||
if mismatch == 0: | ||
return | ||
if mismatch <= diff_pixels: | ||
return | ||
if (mismatch/math.prod(img_a.size)) <= diff_ratio: | ||
return | ||
else: | ||
# Create new test_results folder | ||
test_results_dir.mkdir(parents=True, exist_ok=True) | ||
img_diff.save(f'{test_results_dir}/Diff_{name}') | ||
img_a.save(f'{test_results_dir}/Actual_{name}') | ||
img_b.save(f'{test_results_dir}/Expected_{name}') | ||
print(f"threshold is {threshold}") | ||
print(f"number of mismatch pixels is {mismatch}") | ||
pytest.fail("--> Snapshots DO NOT match!") | ||
|
||
return compare | ||
|
||
|
||
def pytest_addoption(parser: Any) -> None: | ||
group = parser.getgroup("playwright-snapshot", "Playwright Snapshot") | ||
group.addoption( | ||
"--update-snapshots", | ||
action="store_true", | ||
default=False, | ||
help="Update snapshots.", | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,35 @@ | ||
from pathlib import Path | ||
|
||
from setuptools import setup | ||
|
||
|
||
setup( | ||
name="pytest-playwright-snapshot", | ||
author="Kumar Aditya", | ||
author_email="", | ||
description="A pytest wrapper for snapshot testing with playwright", | ||
name="pytest-playwright-visual", | ||
author="Symon Storozhenko", | ||
author_email="[email protected]", | ||
description="A pytest fixture for visual testing with Playwright", | ||
long_description=Path("README.md").read_text(), | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/kumaraditya303/pytest-playwright-snapshot", | ||
packages=["pytest_playwright_snapshot"], | ||
url="https://github.com/symon-storozhenko/pytest-playwright-visual", | ||
packages=["pytest_playwright_visual"], | ||
include_package_data=True, | ||
install_requires=[ | ||
"pytest_playwright>=0.1.2", | ||
"Pillow>=8.2.0", | ||
"pixelmatch==0.2.3", | ||
"pixelmatch>=0.3.0", | ||
], | ||
entry_points={ | ||
"pytest11": ["playwright_snapshot = pytest_playwright_snapshot.plugin"] | ||
"pytest11": ["playwright_visual = pytest_playwright_visual.plugin"] | ||
}, | ||
classifiers=[ | ||
"Programming Language :: Python :: 3", | ||
"Programming Language :: Python :: 3.7", | ||
"Programming Language :: Python :: 3.8", | ||
"Programming Language :: Python :: 3.9", | ||
"Programming Language :: Python :: 3.10", | ||
"License :: OSI Approved :: Apache Software License", | ||
"Operating System :: OS Independent", | ||
"Framework :: Pytest", | ||
], | ||
python_requires=">=3.7", | ||
python_requires=">=3.8", | ||
use_scm_version=True, | ||
setup_requires=["setuptools_scm"], | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't be changing the author nor the project name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hgye are you open to changing this so we can get this merged in?