Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 8 additions & 4 deletions pytest_playwright_visual/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.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")
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
Expand All @@ -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
Expand Down