Skip to content

Commit

Permalink
test: write basic tests of image comparsion with Pillow
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoJM committed Apr 11, 2024
1 parent 9eb95b0 commit 2f8cc02
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__errors__
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions tests/test_match_snapshot_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import os
from shutil import rmtree

import pytest

from match_snapshot import MatchSnapshot


class Element:
def screenshot(self, path):
pass


class TestsMatchSnapshotWithImages(MatchSnapshot):
def test_match_snapshot_same_images(self):
self.snapshot_path = "./tests/__snapshots__"
id = "python_home_page"

with open("./tests/__snapshots__/snapshot_python_home_page.png", "rb") as f1:
with open("./tests/__snapshots__/compare_python_home_page.png", "wb") as f2:
f2.write(f1.read())

self.assert_match_snapshot(Element(), id)

def test_match_snapshot_wrong_image(self):
self.snapshot_path = "./tests/__snapshots__"
self.failed_path = "./tests/__errors__"
id = "python_home_wrong_box"
if os.path.exists("./tests/__errors__"):
rmtree("./tests/__errors__")

with open("./tests/__snapshots__/snapshot_python_home_page.png", "rb") as f1:
with open(
"./tests/__snapshots__/compare_python_home_wrong_box.png", "wb"
) as f2:
f2.write(f1.read())

with pytest.raises(AssertionError):
self.assert_match_snapshot(Element(), id)

assert os.path.exists(f"./tests/__errors__/{id}/comparison.png")
assert os.path.exists(f"./tests/__errors__/{id}/difference_threshold.png")
assert os.path.exists(f"./tests/__errors__/{id}/difference.png")
assert os.path.exists(f"./tests/__errors__/{id}/snapshot.png")
assert os.path.isfile(f"./tests/__errors__/{id}/comparison.png")
assert os.path.isfile(f"./tests/__errors__/{id}/difference_threshold.png")
assert os.path.isfile(f"./tests/__errors__/{id}/difference.png")
assert os.path.isfile(f"./tests/__errors__/{id}/snapshot.png")

if os.path.exists("./tests/__errors__"):
rmtree("./tests/__errors__")

def test_match_snapshot_threshold(self):
self.snapshot_path = "./tests/__snapshots__"
id = "python_home_threshold"

with open("./tests/__snapshots__/snapshot_python_home_page.png", "rb") as f1:
with open(
"./tests/__snapshots__/compare_python_home_threshold.png", "wb"
) as f2:
f2.write(f1.read())

self.assert_match_snapshot(Element(), id)

0 comments on commit 2f8cc02

Please sign in to comment.