pytest-image-snapshot 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pytest-image-snapshot-0.1.0/LICENSE +21 -0
- pytest-image-snapshot-0.1.0/MANIFEST.in +5 -0
- pytest-image-snapshot-0.1.0/PKG-INFO +140 -0
- pytest-image-snapshot-0.1.0/README.md +109 -0
- pytest-image-snapshot-0.1.0/pytest_image_snapshot.egg-info/PKG-INFO +140 -0
- pytest-image-snapshot-0.1.0/pytest_image_snapshot.egg-info/SOURCES.txt +13 -0
- pytest-image-snapshot-0.1.0/pytest_image_snapshot.egg-info/dependency_links.txt +1 -0
- pytest-image-snapshot-0.1.0/pytest_image_snapshot.egg-info/entry_points.txt +2 -0
- pytest-image-snapshot-0.1.0/pytest_image_snapshot.egg-info/requires.txt +2 -0
- pytest-image-snapshot-0.1.0/pytest_image_snapshot.egg-info/top_level.txt +1 -0
- pytest-image-snapshot-0.1.0/pytest_image_snapshot.py +64 -0
- pytest-image-snapshot-0.1.0/setup.cfg +7 -0
- pytest-image-snapshot-0.1.0/setup.py +51 -0
- pytest-image-snapshot-0.1.0/tests/test_image_snapshot.py +39 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Bojan Mihelac
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pytest-image-snapshot
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A pytest plugin for image snapshot management and comparison.
|
|
5
|
+
Home-page: https://github.com/bmihelac/pytest-image-snapshot
|
|
6
|
+
Author: Bojan Mihelac
|
|
7
|
+
Author-email: bojan@informatikamihelac.com
|
|
8
|
+
Maintainer: Bojan Mihelac
|
|
9
|
+
Maintainer-email: bojan@informatikamihelac.com
|
|
10
|
+
License: MIT
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Framework :: Pytest
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Topic :: Software Development :: Testing
|
|
15
|
+
Classifier: Programming Language :: Python
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.5
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.6
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
21
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
22
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
23
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
24
|
+
Classifier: Operating System :: OS Independent
|
|
25
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
26
|
+
Requires-Python: >=3.5
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
Requires-Dist: pytest>=3.5.0
|
|
30
|
+
Requires-Dist: Pillow
|
|
31
|
+
|
|
32
|
+
# pytest-image-snapshot
|
|
33
|
+
|
|
34
|
+
[](https://pypi.org/project/pytest-image-snapshot)
|
|
35
|
+
|
|
36
|
+
[](https://pypi.org/project/pytest-image-snapshot)
|
|
37
|
+
|
|
38
|
+
A pytest plugin for image snapshot management and comparison.
|
|
39
|
+
|
|
40
|
+
------------------------------------------------------------------------
|
|
41
|
+
|
|
42
|
+
## Features
|
|
43
|
+
|
|
44
|
+
- **Image Comparison**: Automatically compares a test-generated image with a pre-stored snapshot, identifying any visual discrepancies.
|
|
45
|
+
- **Snapshot Creation**: If a reference snapshot doesn't exist, the plugin will create it during the test run, making initial setup effortless.
|
|
46
|
+
- **Verbose Mode Display**: Capable of displaying the difference image for quick visual feedback in case of mismatches when running tests with `-v`.
|
|
47
|
+
- **Snapshot Update Option**: Includes a `--image-snapshot-update` flag to update existing snapshots or create new ones, accommodating visual changes in your project.
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
## Requirements
|
|
51
|
+
|
|
52
|
+
- Pillow
|
|
53
|
+
|
|
54
|
+
## Installation
|
|
55
|
+
|
|
56
|
+
You can install \"pytest-image-snapshot\" via
|
|
57
|
+
[pip](https://pypi.org/project/pip/) from
|
|
58
|
+
[PyPI](https://pypi.org/project):
|
|
59
|
+
|
|
60
|
+
$ pip install pytest-image-snapshot
|
|
61
|
+
|
|
62
|
+
## Pytest Image Snapshot Usage Example
|
|
63
|
+
|
|
64
|
+
The `image_snapshot` fixture is designed for visual regression testing in pytest. It compares a generated image in your tests with a stored reference image (snapshot). If the snapshot doesn't exist, it will be automatically created. This makes the fixture ideal for both creating initial snapshots and for ongoing comparison in visual tests.
|
|
65
|
+
|
|
66
|
+
### Usage
|
|
67
|
+
|
|
68
|
+
Here's a example of how to utilize the `image_snapshot` fixture:
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from PIL import Image
|
|
72
|
+
|
|
73
|
+
def test_image(image_snapshot):
|
|
74
|
+
# Create a new white image of 100x100 pixels
|
|
75
|
+
image = Image.new('RGB', (100, 100), 'white')
|
|
76
|
+
# Compare it to the snapshot stored in test_snapshots/test.png
|
|
77
|
+
# If test_snapshots/test.png does not exist, it will be created
|
|
78
|
+
image_snapshot(image, "test_snapshots/test.png")
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
> **⚠️ Warning:**
|
|
82
|
+
>
|
|
83
|
+
> The `image_snapshot` fixture does not automatically create directories for storing image snapshots. Ensure that the necessary directories (e.g., `test_snapshots/`) are created in your project structure before running tests.
|
|
84
|
+
|
|
85
|
+
### Verbose Mode (`-v` or `--verbose`)
|
|
86
|
+
|
|
87
|
+
The verbose mode enhances the output detail for `image_snapshot` tests:
|
|
88
|
+
- `-v`: Displays the 'diff' image when there's a mismatch.
|
|
89
|
+
- `-vv`: Shows all three images - 'diff', 'original', and 'current' for a comprehensive comparison.
|
|
90
|
+
|
|
91
|
+
This feature assists in quickly identifying and analyzing visual differences during test failures.
|
|
92
|
+
|
|
93
|
+
### Updating Snapshots (`--image-snapshot-update`)
|
|
94
|
+
|
|
95
|
+
Use the `--image-snapshot-update` flag to update or create new reference snapshots. This is useful for incorporating intentional visual changes into your tests, ensuring that your snapshots always reflect the current expected state.
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
pytest --image-snapshot-update
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Example
|
|
102
|
+
|
|
103
|
+
Visual regression test for home page with [playwright](https://playwright.dev/python/docs/intro):
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
from io import BytesIO
|
|
107
|
+
|
|
108
|
+
def test_homepage(page: Page, image_snapshot):
|
|
109
|
+
page.goto("http://localhost:8000")
|
|
110
|
+
# convert screenshot to image
|
|
111
|
+
screenshot = Image.open(BytesIO(page.screenshot()))
|
|
112
|
+
is_image_equal(screenshot, "test_snapshots/homepage.png")
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Contributing
|
|
116
|
+
|
|
117
|
+
Contributions are very welcome. Tests can be run with
|
|
118
|
+
[tox](https://tox.readthedocs.io/en/latest/), please ensure the coverage
|
|
119
|
+
at least stays the same before you submit a pull request.
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
Distributed under the terms of the
|
|
124
|
+
[MIT](http://opensource.org/licenses/MIT) license,
|
|
125
|
+
\"pytest-image-snapshot\" is free and open source software
|
|
126
|
+
|
|
127
|
+
## Issues
|
|
128
|
+
|
|
129
|
+
If you encounter any problems, please [file an
|
|
130
|
+
issue](https://github.com/bmihelac/pytest-image-snapshot/issues) along
|
|
131
|
+
with a detailed description.
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
This [pytest](https://github.com/pytest-dev/pytest) plugin was generated
|
|
136
|
+
with [Cookiecutter](https://github.com/audreyr/cookiecutter) along with
|
|
137
|
+
[\@hackebrot](https://github.com/hackebrot)\'s
|
|
138
|
+
[cookiecutter-pytest-plugin](https://github.com/pytest-dev/cookiecutter-pytest-plugin)
|
|
139
|
+
template.
|
|
140
|
+
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# pytest-image-snapshot
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/pytest-image-snapshot)
|
|
4
|
+
|
|
5
|
+
[](https://pypi.org/project/pytest-image-snapshot)
|
|
6
|
+
|
|
7
|
+
A pytest plugin for image snapshot management and comparison.
|
|
8
|
+
|
|
9
|
+
------------------------------------------------------------------------
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Image Comparison**: Automatically compares a test-generated image with a pre-stored snapshot, identifying any visual discrepancies.
|
|
14
|
+
- **Snapshot Creation**: If a reference snapshot doesn't exist, the plugin will create it during the test run, making initial setup effortless.
|
|
15
|
+
- **Verbose Mode Display**: Capable of displaying the difference image for quick visual feedback in case of mismatches when running tests with `-v`.
|
|
16
|
+
- **Snapshot Update Option**: Includes a `--image-snapshot-update` flag to update existing snapshots or create new ones, accommodating visual changes in your project.
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
## Requirements
|
|
20
|
+
|
|
21
|
+
- Pillow
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
You can install \"pytest-image-snapshot\" via
|
|
26
|
+
[pip](https://pypi.org/project/pip/) from
|
|
27
|
+
[PyPI](https://pypi.org/project):
|
|
28
|
+
|
|
29
|
+
$ pip install pytest-image-snapshot
|
|
30
|
+
|
|
31
|
+
## Pytest Image Snapshot Usage Example
|
|
32
|
+
|
|
33
|
+
The `image_snapshot` fixture is designed for visual regression testing in pytest. It compares a generated image in your tests with a stored reference image (snapshot). If the snapshot doesn't exist, it will be automatically created. This makes the fixture ideal for both creating initial snapshots and for ongoing comparison in visual tests.
|
|
34
|
+
|
|
35
|
+
### Usage
|
|
36
|
+
|
|
37
|
+
Here's a example of how to utilize the `image_snapshot` fixture:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from PIL import Image
|
|
41
|
+
|
|
42
|
+
def test_image(image_snapshot):
|
|
43
|
+
# Create a new white image of 100x100 pixels
|
|
44
|
+
image = Image.new('RGB', (100, 100), 'white')
|
|
45
|
+
# Compare it to the snapshot stored in test_snapshots/test.png
|
|
46
|
+
# If test_snapshots/test.png does not exist, it will be created
|
|
47
|
+
image_snapshot(image, "test_snapshots/test.png")
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
> **⚠️ Warning:**
|
|
51
|
+
>
|
|
52
|
+
> The `image_snapshot` fixture does not automatically create directories for storing image snapshots. Ensure that the necessary directories (e.g., `test_snapshots/`) are created in your project structure before running tests.
|
|
53
|
+
|
|
54
|
+
### Verbose Mode (`-v` or `--verbose`)
|
|
55
|
+
|
|
56
|
+
The verbose mode enhances the output detail for `image_snapshot` tests:
|
|
57
|
+
- `-v`: Displays the 'diff' image when there's a mismatch.
|
|
58
|
+
- `-vv`: Shows all three images - 'diff', 'original', and 'current' for a comprehensive comparison.
|
|
59
|
+
|
|
60
|
+
This feature assists in quickly identifying and analyzing visual differences during test failures.
|
|
61
|
+
|
|
62
|
+
### Updating Snapshots (`--image-snapshot-update`)
|
|
63
|
+
|
|
64
|
+
Use the `--image-snapshot-update` flag to update or create new reference snapshots. This is useful for incorporating intentional visual changes into your tests, ensuring that your snapshots always reflect the current expected state.
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
pytest --image-snapshot-update
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Example
|
|
71
|
+
|
|
72
|
+
Visual regression test for home page with [playwright](https://playwright.dev/python/docs/intro):
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from io import BytesIO
|
|
76
|
+
|
|
77
|
+
def test_homepage(page: Page, image_snapshot):
|
|
78
|
+
page.goto("http://localhost:8000")
|
|
79
|
+
# convert screenshot to image
|
|
80
|
+
screenshot = Image.open(BytesIO(page.screenshot()))
|
|
81
|
+
is_image_equal(screenshot, "test_snapshots/homepage.png")
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Contributing
|
|
85
|
+
|
|
86
|
+
Contributions are very welcome. Tests can be run with
|
|
87
|
+
[tox](https://tox.readthedocs.io/en/latest/), please ensure the coverage
|
|
88
|
+
at least stays the same before you submit a pull request.
|
|
89
|
+
|
|
90
|
+
## License
|
|
91
|
+
|
|
92
|
+
Distributed under the terms of the
|
|
93
|
+
[MIT](http://opensource.org/licenses/MIT) license,
|
|
94
|
+
\"pytest-image-snapshot\" is free and open source software
|
|
95
|
+
|
|
96
|
+
## Issues
|
|
97
|
+
|
|
98
|
+
If you encounter any problems, please [file an
|
|
99
|
+
issue](https://github.com/bmihelac/pytest-image-snapshot/issues) along
|
|
100
|
+
with a detailed description.
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
This [pytest](https://github.com/pytest-dev/pytest) plugin was generated
|
|
105
|
+
with [Cookiecutter](https://github.com/audreyr/cookiecutter) along with
|
|
106
|
+
[\@hackebrot](https://github.com/hackebrot)\'s
|
|
107
|
+
[cookiecutter-pytest-plugin](https://github.com/pytest-dev/cookiecutter-pytest-plugin)
|
|
108
|
+
template.
|
|
109
|
+
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pytest-image-snapshot
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A pytest plugin for image snapshot management and comparison.
|
|
5
|
+
Home-page: https://github.com/bmihelac/pytest-image-snapshot
|
|
6
|
+
Author: Bojan Mihelac
|
|
7
|
+
Author-email: bojan@informatikamihelac.com
|
|
8
|
+
Maintainer: Bojan Mihelac
|
|
9
|
+
Maintainer-email: bojan@informatikamihelac.com
|
|
10
|
+
License: MIT
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Framework :: Pytest
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Topic :: Software Development :: Testing
|
|
15
|
+
Classifier: Programming Language :: Python
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.5
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.6
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
21
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
22
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
23
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
24
|
+
Classifier: Operating System :: OS Independent
|
|
25
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
26
|
+
Requires-Python: >=3.5
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
Requires-Dist: pytest>=3.5.0
|
|
30
|
+
Requires-Dist: Pillow
|
|
31
|
+
|
|
32
|
+
# pytest-image-snapshot
|
|
33
|
+
|
|
34
|
+
[](https://pypi.org/project/pytest-image-snapshot)
|
|
35
|
+
|
|
36
|
+
[](https://pypi.org/project/pytest-image-snapshot)
|
|
37
|
+
|
|
38
|
+
A pytest plugin for image snapshot management and comparison.
|
|
39
|
+
|
|
40
|
+
------------------------------------------------------------------------
|
|
41
|
+
|
|
42
|
+
## Features
|
|
43
|
+
|
|
44
|
+
- **Image Comparison**: Automatically compares a test-generated image with a pre-stored snapshot, identifying any visual discrepancies.
|
|
45
|
+
- **Snapshot Creation**: If a reference snapshot doesn't exist, the plugin will create it during the test run, making initial setup effortless.
|
|
46
|
+
- **Verbose Mode Display**: Capable of displaying the difference image for quick visual feedback in case of mismatches when running tests with `-v`.
|
|
47
|
+
- **Snapshot Update Option**: Includes a `--image-snapshot-update` flag to update existing snapshots or create new ones, accommodating visual changes in your project.
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
## Requirements
|
|
51
|
+
|
|
52
|
+
- Pillow
|
|
53
|
+
|
|
54
|
+
## Installation
|
|
55
|
+
|
|
56
|
+
You can install \"pytest-image-snapshot\" via
|
|
57
|
+
[pip](https://pypi.org/project/pip/) from
|
|
58
|
+
[PyPI](https://pypi.org/project):
|
|
59
|
+
|
|
60
|
+
$ pip install pytest-image-snapshot
|
|
61
|
+
|
|
62
|
+
## Pytest Image Snapshot Usage Example
|
|
63
|
+
|
|
64
|
+
The `image_snapshot` fixture is designed for visual regression testing in pytest. It compares a generated image in your tests with a stored reference image (snapshot). If the snapshot doesn't exist, it will be automatically created. This makes the fixture ideal for both creating initial snapshots and for ongoing comparison in visual tests.
|
|
65
|
+
|
|
66
|
+
### Usage
|
|
67
|
+
|
|
68
|
+
Here's a example of how to utilize the `image_snapshot` fixture:
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from PIL import Image
|
|
72
|
+
|
|
73
|
+
def test_image(image_snapshot):
|
|
74
|
+
# Create a new white image of 100x100 pixels
|
|
75
|
+
image = Image.new('RGB', (100, 100), 'white')
|
|
76
|
+
# Compare it to the snapshot stored in test_snapshots/test.png
|
|
77
|
+
# If test_snapshots/test.png does not exist, it will be created
|
|
78
|
+
image_snapshot(image, "test_snapshots/test.png")
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
> **⚠️ Warning:**
|
|
82
|
+
>
|
|
83
|
+
> The `image_snapshot` fixture does not automatically create directories for storing image snapshots. Ensure that the necessary directories (e.g., `test_snapshots/`) are created in your project structure before running tests.
|
|
84
|
+
|
|
85
|
+
### Verbose Mode (`-v` or `--verbose`)
|
|
86
|
+
|
|
87
|
+
The verbose mode enhances the output detail for `image_snapshot` tests:
|
|
88
|
+
- `-v`: Displays the 'diff' image when there's a mismatch.
|
|
89
|
+
- `-vv`: Shows all three images - 'diff', 'original', and 'current' for a comprehensive comparison.
|
|
90
|
+
|
|
91
|
+
This feature assists in quickly identifying and analyzing visual differences during test failures.
|
|
92
|
+
|
|
93
|
+
### Updating Snapshots (`--image-snapshot-update`)
|
|
94
|
+
|
|
95
|
+
Use the `--image-snapshot-update` flag to update or create new reference snapshots. This is useful for incorporating intentional visual changes into your tests, ensuring that your snapshots always reflect the current expected state.
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
pytest --image-snapshot-update
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Example
|
|
102
|
+
|
|
103
|
+
Visual regression test for home page with [playwright](https://playwright.dev/python/docs/intro):
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
from io import BytesIO
|
|
107
|
+
|
|
108
|
+
def test_homepage(page: Page, image_snapshot):
|
|
109
|
+
page.goto("http://localhost:8000")
|
|
110
|
+
# convert screenshot to image
|
|
111
|
+
screenshot = Image.open(BytesIO(page.screenshot()))
|
|
112
|
+
is_image_equal(screenshot, "test_snapshots/homepage.png")
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Contributing
|
|
116
|
+
|
|
117
|
+
Contributions are very welcome. Tests can be run with
|
|
118
|
+
[tox](https://tox.readthedocs.io/en/latest/), please ensure the coverage
|
|
119
|
+
at least stays the same before you submit a pull request.
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
Distributed under the terms of the
|
|
124
|
+
[MIT](http://opensource.org/licenses/MIT) license,
|
|
125
|
+
\"pytest-image-snapshot\" is free and open source software
|
|
126
|
+
|
|
127
|
+
## Issues
|
|
128
|
+
|
|
129
|
+
If you encounter any problems, please [file an
|
|
130
|
+
issue](https://github.com/bmihelac/pytest-image-snapshot/issues) along
|
|
131
|
+
with a detailed description.
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
This [pytest](https://github.com/pytest-dev/pytest) plugin was generated
|
|
136
|
+
with [Cookiecutter](https://github.com/audreyr/cookiecutter) along with
|
|
137
|
+
[\@hackebrot](https://github.com/hackebrot)\'s
|
|
138
|
+
[cookiecutter-pytest-plugin](https://github.com/pytest-dev/cookiecutter-pytest-plugin)
|
|
139
|
+
template.
|
|
140
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
pytest_image_snapshot.py
|
|
5
|
+
setup.cfg
|
|
6
|
+
setup.py
|
|
7
|
+
pytest_image_snapshot.egg-info/PKG-INFO
|
|
8
|
+
pytest_image_snapshot.egg-info/SOURCES.txt
|
|
9
|
+
pytest_image_snapshot.egg-info/dependency_links.txt
|
|
10
|
+
pytest_image_snapshot.egg-info/entry_points.txt
|
|
11
|
+
pytest_image_snapshot.egg-info/requires.txt
|
|
12
|
+
pytest_image_snapshot.egg-info/top_level.txt
|
|
13
|
+
tests/test_image_snapshot.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pytest_image_snapshot
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
from PIL import Image, ImageChops
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class ImageMismatchError(AssertionError):
|
|
8
|
+
"""Exception raised when images do not match."""
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def pytest_addoption(parser):
|
|
12
|
+
parser.addoption(
|
|
13
|
+
"--image-snapshot-update", action="store_true", help="Update image snapshots"
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def extend_to_match_size_rgba(img1, img2):
|
|
18
|
+
"""
|
|
19
|
+
Extend the smaller image to match the size of the larger one using RGBA mode
|
|
20
|
+
with a transparent background.
|
|
21
|
+
|
|
22
|
+
:param img1: First PIL Image object in RGBA mode
|
|
23
|
+
:param img2: Second PIL Image object in RGBA mode
|
|
24
|
+
:return: Tuple of two PIL Image objects with the same size
|
|
25
|
+
"""
|
|
26
|
+
max_width = max(img1.width, img2.width)
|
|
27
|
+
max_height = max(img1.height, img2.height)
|
|
28
|
+
|
|
29
|
+
def extend_image(img):
|
|
30
|
+
if img.width == max_width and img.height == max_height:
|
|
31
|
+
return img
|
|
32
|
+
new_img = Image.new("RGBA", (max_width, max_height), (0, 0, 0, 0))
|
|
33
|
+
new_img.paste(img, (0, 0), img)
|
|
34
|
+
return new_img
|
|
35
|
+
|
|
36
|
+
return extend_image(img1.convert("RGBA")), extend_image(img2.convert("RGBA"))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@pytest.fixture
|
|
40
|
+
def image_snapshot(request):
|
|
41
|
+
def _image_snapshot(img, img_path):
|
|
42
|
+
config = request.config
|
|
43
|
+
update_snapshots = config.getoption("--image-snapshot-update")
|
|
44
|
+
|
|
45
|
+
img_path = Path(img_path)
|
|
46
|
+
if not update_snapshots and img_path.exists():
|
|
47
|
+
src_image = Image.open(img_path)
|
|
48
|
+
img_1, img_2 = extend_to_match_size_rgba(img, src_image)
|
|
49
|
+
diff = ImageChops.difference(img_1, img_2)
|
|
50
|
+
if diff.getbbox():
|
|
51
|
+
if config.option.verbose:
|
|
52
|
+
diff.show(title="diff")
|
|
53
|
+
if config.option.verbose > 1:
|
|
54
|
+
src_image.show(title="original")
|
|
55
|
+
img.show(title="new")
|
|
56
|
+
raise ImageMismatchError(
|
|
57
|
+
f"Image does not match the snapshot stored in {img_path}"
|
|
58
|
+
)
|
|
59
|
+
else:
|
|
60
|
+
return
|
|
61
|
+
img.save(img_path)
|
|
62
|
+
return
|
|
63
|
+
|
|
64
|
+
return _image_snapshot
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
import codecs
|
|
6
|
+
from setuptools import setup
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def read(fname):
|
|
10
|
+
file_path = os.path.join(os.path.dirname(__file__), fname)
|
|
11
|
+
return codecs.open(file_path, encoding='utf-8').read()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
setup(
|
|
15
|
+
name='pytest-image-snapshot',
|
|
16
|
+
version='0.1.0',
|
|
17
|
+
author='Bojan Mihelac',
|
|
18
|
+
author_email='bojan@informatikamihelac.com',
|
|
19
|
+
maintainer='Bojan Mihelac',
|
|
20
|
+
maintainer_email='bojan@informatikamihelac.com',
|
|
21
|
+
license='MIT',
|
|
22
|
+
url='https://github.com/bmihelac/pytest-image-snapshot',
|
|
23
|
+
description='A pytest plugin for image snapshot management and comparison.',
|
|
24
|
+
long_description=read('README.md'),
|
|
25
|
+
long_description_content_type='text/markdown',
|
|
26
|
+
py_modules=['pytest_image_snapshot'],
|
|
27
|
+
python_requires='>=3.5',
|
|
28
|
+
install_requires=['pytest>=3.5.0', 'Pillow'],
|
|
29
|
+
classifiers=[
|
|
30
|
+
'Development Status :: 4 - Beta',
|
|
31
|
+
'Framework :: Pytest',
|
|
32
|
+
'Intended Audience :: Developers',
|
|
33
|
+
'Topic :: Software Development :: Testing',
|
|
34
|
+
'Programming Language :: Python',
|
|
35
|
+
'Programming Language :: Python :: 3',
|
|
36
|
+
'Programming Language :: Python :: 3.5',
|
|
37
|
+
'Programming Language :: Python :: 3.6',
|
|
38
|
+
'Programming Language :: Python :: 3.7',
|
|
39
|
+
'Programming Language :: Python :: 3.8',
|
|
40
|
+
'Programming Language :: Python :: 3 :: Only',
|
|
41
|
+
'Programming Language :: Python :: Implementation :: CPython',
|
|
42
|
+
'Programming Language :: Python :: Implementation :: PyPy',
|
|
43
|
+
'Operating System :: OS Independent',
|
|
44
|
+
'License :: OSI Approved :: MIT License',
|
|
45
|
+
],
|
|
46
|
+
entry_points={
|
|
47
|
+
'pytest11': [
|
|
48
|
+
'image-snapshot = pytest_image_snapshot',
|
|
49
|
+
],
|
|
50
|
+
},
|
|
51
|
+
)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
from PIL import Image
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@pytest.fixture
|
|
8
|
+
def test_image(pytester):
|
|
9
|
+
img = Image.new("RGB", (100, 100), "white")
|
|
10
|
+
filename = pytester.path.joinpath("white.png")
|
|
11
|
+
img.save(filename, format="PNG")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def test_image_snapshot_fixture(pytester, test_image):
|
|
15
|
+
pytester.makepyfile(
|
|
16
|
+
"""
|
|
17
|
+
from PIL import Image
|
|
18
|
+
import pytest
|
|
19
|
+
|
|
20
|
+
def test_image(image_snapshot):
|
|
21
|
+
image = Image.new('RGB', (100, 100), 'white')
|
|
22
|
+
image_snapshot(image, "white.png")
|
|
23
|
+
|
|
24
|
+
def test_different_image(image_snapshot):
|
|
25
|
+
image = Image.new('RGB', (150, 150), 'white')
|
|
26
|
+
with pytest.raises(AssertionError):
|
|
27
|
+
image_snapshot(image, "white.png")
|
|
28
|
+
"""
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
result = pytester.runpytest("-v")
|
|
32
|
+
|
|
33
|
+
result.stdout.fnmatch_lines(
|
|
34
|
+
[
|
|
35
|
+
"*::test_image PASSED*",
|
|
36
|
+
]
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
assert result.ret == 0
|