From 48ec768f34f7e607dc50596a277d61142fe855c7 Mon Sep 17 00:00:00 2001 From: Andrzej Klajnert Date: Mon, 14 Nov 2022 14:59:24 +0100 Subject: [PATCH 1/2] Allow to change the snapshots path --- README.md | 11 +++++++++++ pytest_playwright_visual/plugin.py | 12 ++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ed341a1..98fb68b 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,17 @@ In case of a mismatch, `snapshot_tests_failures` folder will be created with `Ac - `threshold` - sets the threshold for the comparison of the screenshots:`0` to `1`. Default is `0.1` - `name` - `.png` extensions only. Default is `test_name[browser][os].png` (recommended) - `fail_fast` - If `True`, will fail after first different pixel. `False` by default + +## Change snapshots path + +You can change the default path where snapshots are stored by setting `pytest.snapshots_path` and/or +`pytest.snapshot_failures_path` value in `pytest_configure()` hook in your root `conftest.py`: +```python +def pytest_configure(): + pytest.snapshots_path = Path.cwd() / "snapshots" + pytest.snapshot_failures_path = Path.cwd() / "snapshot_failures" +``` + ## License Apache 2.0 LICENSE diff --git a/pytest_playwright_visual/plugin.py b/pytest_playwright_visual/plugin.py index d287e8b..07f63cc 100644 --- a/pytest_playwright_visual/plugin.py +++ b/pytest_playwright_visual/plugin.py @@ -3,7 +3,7 @@ import shutil from io import BytesIO from pathlib import Path -from typing import Any, Callable +from typing import Any, Callable, Optional import pytest from PIL import Image from pixelmatch.contrib.PIL import pixelmatch @@ -14,10 +14,13 @@ def assert_snapshot(pytestconfig: Any, request: Any, browser_name: str) -> Calla test_name = f"{str(Path(request.node.name))}[{str(sys.platform)}]" test_dir = str(Path(request.node.name)).split('[', 1)[0] + snapshots_path = pytest.snapshots_path if hasattr(pytest, "snapshots_path") else None + snapshot_failures_path = pytest.snapshots_path if hasattr(pytest, "snapshot_failures_path") else None + def compare(img: bytes, *, 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 = ( + filepath = snapshots_path or ( Path(request.node.fspath).parent.resolve() / 'snapshots' / test_file_name @@ -26,8 +29,9 @@ def compare(img: bytes, *, threshold: float = 0.1, name=f'{test_name}.png', fail 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") + results_dir_name = snapshot_failures_path or ( + 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 From d9bcb97b41abc698063aa99bd8ae7a59129d769e Mon Sep 17 00:00:00 2001 From: Andrzej Klajnert Date: Mon, 14 Nov 2022 18:44:46 +0100 Subject: [PATCH 2/2] Update plugin.py --- pytest_playwright_visual/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest_playwright_visual/plugin.py b/pytest_playwright_visual/plugin.py index 07f63cc..b1fa4fb 100644 --- a/pytest_playwright_visual/plugin.py +++ b/pytest_playwright_visual/plugin.py @@ -15,7 +15,7 @@ def assert_snapshot(pytestconfig: Any, request: Any, browser_name: str) -> Calla test_dir = str(Path(request.node.name)).split('[', 1)[0] snapshots_path = pytest.snapshots_path if hasattr(pytest, "snapshots_path") else None - snapshot_failures_path = pytest.snapshots_path if hasattr(pytest, "snapshot_failures_path") else None + snapshot_failures_path = pytest.snapshot_failures_path if hasattr(pytest, "snapshot_failures_path") else None def compare(img: bytes, *, threshold: float = 0.1, name=f'{test_name}.png', fail_fast=False) -> None: update_snapshot = pytestconfig.getoption("--update-snapshots")