pytest-remaster 0.0.0__py3-none-any.whl
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_remaster/__init__.py +13 -0
- pytest_remaster/core.py +143 -0
- pytest_remaster/plugin.py +54 -0
- pytest_remaster-0.0.0.dist-info/METADATA +48 -0
- pytest_remaster-0.0.0.dist-info/RECORD +9 -0
- pytest_remaster-0.0.0.dist-info/WHEEL +5 -0
- pytest_remaster-0.0.0.dist-info/entry_points.txt +2 -0
- pytest_remaster-0.0.0.dist-info/licenses/LICENSE +21 -0
- pytest_remaster-0.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""Golden master testing for pytest with automatic regeneration."""
|
|
2
|
+
|
|
3
|
+
from pytest_remaster.core import ( # pragma: no cover
|
|
4
|
+
GoldenMaster,
|
|
5
|
+
discover_test_cases,
|
|
6
|
+
discover_test_files,
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"GoldenMaster",
|
|
11
|
+
"discover_test_cases",
|
|
12
|
+
"discover_test_files",
|
|
13
|
+
] # pragma: no cover
|
pytest_remaster/core.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"""Core golden master comparison and discovery logic."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations # pragma: no cover
|
|
4
|
+
|
|
5
|
+
import difflib # pragma: no cover
|
|
6
|
+
import re # pragma: no cover
|
|
7
|
+
from collections.abc import Callable # pragma: no cover
|
|
8
|
+
from pathlib import Path # pragma: no cover
|
|
9
|
+
from typing import Any # pragma: no cover
|
|
10
|
+
|
|
11
|
+
import pytest # pragma: no cover
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class GoldenMaster: # pragma: no cover
|
|
15
|
+
"""Golden master comparison with optional auto-regeneration."""
|
|
16
|
+
|
|
17
|
+
def __init__(self, remaster: bool) -> None: # pragma: no cover
|
|
18
|
+
self._remaster = remaster
|
|
19
|
+
|
|
20
|
+
def check( # pragma: no cover
|
|
21
|
+
self,
|
|
22
|
+
actual: Any | Callable[[], Any],
|
|
23
|
+
expected_path: str | Path,
|
|
24
|
+
serializer: Callable[[Any], str] = str,
|
|
25
|
+
) -> None:
|
|
26
|
+
"""Compare actual output against an expected file.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
actual: The actual value, or a callable that produces it.
|
|
30
|
+
expected_path: Path to the expected output file.
|
|
31
|
+
serializer: Converts actual value to string. Default: str().
|
|
32
|
+
"""
|
|
33
|
+
expected_path = Path(expected_path)
|
|
34
|
+
if callable(actual) and not isinstance(actual, str):
|
|
35
|
+
actual = actual()
|
|
36
|
+
actual_str = serializer(actual).rstrip()
|
|
37
|
+
|
|
38
|
+
try:
|
|
39
|
+
expected_str = expected_path.read_text(encoding="utf-8").rstrip()
|
|
40
|
+
except FileNotFoundError:
|
|
41
|
+
expected_str = None
|
|
42
|
+
|
|
43
|
+
if expected_str is not None and actual_str == expected_str:
|
|
44
|
+
return
|
|
45
|
+
|
|
46
|
+
if self._remaster:
|
|
47
|
+
expected_path.parent.mkdir(parents=True, exist_ok=True)
|
|
48
|
+
expected_path.write_text(actual_str + "\n", encoding="utf-8")
|
|
49
|
+
action = "created" if expected_str is None else "updated"
|
|
50
|
+
pytest.fail(
|
|
51
|
+
f"Golden master {action} at {expected_path}, "
|
|
52
|
+
f"please review and relaunch.",
|
|
53
|
+
pytrace=False,
|
|
54
|
+
)
|
|
55
|
+
else:
|
|
56
|
+
if expected_str is None:
|
|
57
|
+
pytest.fail(
|
|
58
|
+
f"Expected file {expected_path} does not exist. "
|
|
59
|
+
f"Run with --remaster to create it.",
|
|
60
|
+
pytrace=False,
|
|
61
|
+
)
|
|
62
|
+
diff = difflib.unified_diff(
|
|
63
|
+
expected_str.splitlines(keepends=True),
|
|
64
|
+
actual_str.splitlines(keepends=True),
|
|
65
|
+
fromfile=str(expected_path),
|
|
66
|
+
tofile="actual",
|
|
67
|
+
)
|
|
68
|
+
pytest.fail(
|
|
69
|
+
f"Golden master mismatch at {expected_path}:\n{''.join(diff)}",
|
|
70
|
+
pytrace=False,
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
def check_all( # pragma: no cover
|
|
74
|
+
self,
|
|
75
|
+
*actuals: Any | Callable[[], list[Any]],
|
|
76
|
+
directory: str | Path,
|
|
77
|
+
serializer: Callable[[Any], str] = str,
|
|
78
|
+
) -> None:
|
|
79
|
+
"""Compare multiple actuals against result_0, result_1, ... files.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
*actuals: Values to compare, or a single callable returning a list.
|
|
83
|
+
directory: Directory containing result_N files.
|
|
84
|
+
serializer: Converts each value to string. Default: str().
|
|
85
|
+
"""
|
|
86
|
+
directory = Path(directory)
|
|
87
|
+
|
|
88
|
+
if (
|
|
89
|
+
len(actuals) == 1
|
|
90
|
+
and callable(actuals[0])
|
|
91
|
+
and not isinstance(actuals[0], str)
|
|
92
|
+
):
|
|
93
|
+
actuals = tuple(actuals[0]())
|
|
94
|
+
|
|
95
|
+
existing = sorted(
|
|
96
|
+
(p for p in directory.iterdir() if re.match(r"result_\d+$", p.name)),
|
|
97
|
+
key=lambda p: int(p.name.split("_")[1]),
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
if self._remaster and len(actuals) != len(existing):
|
|
101
|
+
for extra in existing[len(actuals) :]:
|
|
102
|
+
extra.unlink()
|
|
103
|
+
|
|
104
|
+
for i, actual in enumerate(actuals):
|
|
105
|
+
self.check(actual, directory / f"result_{i}", serializer=serializer)
|
|
106
|
+
|
|
107
|
+
if not self._remaster and len(actuals) < len(existing):
|
|
108
|
+
extra_files = [p.name for p in existing[len(actuals) :]]
|
|
109
|
+
pytest.fail(
|
|
110
|
+
f"Expected {len(existing)} results but got {len(actuals)}. "
|
|
111
|
+
f"Extra files: {extra_files}. Run with --remaster to clean up.",
|
|
112
|
+
pytrace=False,
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def discover_test_cases(base_dir: str | Path) -> list[Path]: # pragma: no cover
|
|
117
|
+
"""Find leaf directories (containing only files) under base_dir.
|
|
118
|
+
|
|
119
|
+
Suitable for use with ``@pytest.mark.parametrize``.
|
|
120
|
+
Returns absolute paths sorted alphabetically.
|
|
121
|
+
"""
|
|
122
|
+
base_dir = Path(base_dir)
|
|
123
|
+
result: list[Path] = []
|
|
124
|
+
for entry in sorted(base_dir.iterdir()):
|
|
125
|
+
if not entry.is_dir(): # pragma: no cover
|
|
126
|
+
continue
|
|
127
|
+
if all(f.is_file() for f in entry.iterdir()):
|
|
128
|
+
result.append(entry)
|
|
129
|
+
else:
|
|
130
|
+
result.extend(discover_test_cases(entry))
|
|
131
|
+
return result
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def discover_test_files(
|
|
135
|
+
base_dir: str | Path, pattern: str = "*.py"
|
|
136
|
+
) -> list[Path]: # pragma: no cover
|
|
137
|
+
"""Find files matching a glob pattern under base_dir.
|
|
138
|
+
|
|
139
|
+
Suitable for use with ``@pytest.mark.parametrize``.
|
|
140
|
+
Returns absolute paths sorted alphabetically.
|
|
141
|
+
"""
|
|
142
|
+
base_dir = Path(base_dir)
|
|
143
|
+
return sorted(base_dir.rglob(pattern))
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""Pytest plugin for golden master testing with automatic regeneration."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations # pragma: no cover
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING # pragma: no cover
|
|
6
|
+
|
|
7
|
+
import pytest # pragma: no cover
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING: # pragma: no cover
|
|
10
|
+
from pytest_remaster.core import GoldenMaster
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def pytest_addoption(parser: pytest.Parser) -> None: # pragma: no cover
|
|
14
|
+
group = parser.getgroup(
|
|
15
|
+
"remaster", "Golden master testing with automatic regeneration"
|
|
16
|
+
)
|
|
17
|
+
group.addoption(
|
|
18
|
+
"--remaster",
|
|
19
|
+
action="store_true",
|
|
20
|
+
dest="remaster",
|
|
21
|
+
default=None,
|
|
22
|
+
help="Regenerate golden master files when comparison fails.",
|
|
23
|
+
)
|
|
24
|
+
group.addoption(
|
|
25
|
+
"--no-remaster",
|
|
26
|
+
action="store_false",
|
|
27
|
+
dest="remaster",
|
|
28
|
+
help="Compare against golden master files without regenerating.",
|
|
29
|
+
)
|
|
30
|
+
parser.addini(
|
|
31
|
+
"remaster-by-default",
|
|
32
|
+
type="bool",
|
|
33
|
+
default=True,
|
|
34
|
+
help="Whether to regenerate golden master files by default (default: True).",
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@pytest.fixture # pragma: no cover
|
|
39
|
+
def remaster(request: pytest.FixtureRequest) -> bool: # pragma: no cover
|
|
40
|
+
"""Whether tests should regenerate golden master files."""
|
|
41
|
+
if (cli := request.config.getoption("remaster")) is not None:
|
|
42
|
+
return bool(cli)
|
|
43
|
+
result: bool = request.config.getini("remaster-by-default")
|
|
44
|
+
return result
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@pytest.fixture # pragma: no cover
|
|
48
|
+
def golden_master(
|
|
49
|
+
remaster: bool, # pylint: disable=redefined-outer-name
|
|
50
|
+
) -> GoldenMaster: # pragma: no cover
|
|
51
|
+
"""Golden master comparison fixture."""
|
|
52
|
+
from pytest_remaster.core import GoldenMaster # pylint: disable=import-outside-toplevel
|
|
53
|
+
|
|
54
|
+
return GoldenMaster(remaster=remaster)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pytest-remaster
|
|
3
|
+
Version: 0.0.0
|
|
4
|
+
Summary: Golden master testing for pytest with automatic regeneration.
|
|
5
|
+
Author: Pierre Sassoulas
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Pierre-Sassoulas/pytest-remaster
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Framework :: Pytest
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Topic :: Software Development :: Testing
|
|
19
|
+
Requires-Python: >=3.9
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: pytest>=7
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: pre-commit; extra == "dev"
|
|
25
|
+
Requires-Dist: pylint>4; extra == "dev"
|
|
26
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
[](https://badge.fury.io/py/pytest-remaster)
|
|
30
|
+
[](https://pypi.org/project/pytest-remaster/)
|
|
31
|
+
[](https://pypi.org/project/pytest-remaster/)
|
|
32
|
+
|
|
33
|
+
# pytest-remaster
|
|
34
|
+
|
|
35
|
+
Framework for golden master testing for pytest with automatic regeneration of the golden
|
|
36
|
+
master.
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
By default, golden master files are automatically regenerated when a comparison fails.
|
|
41
|
+
Use `--no-remaster` to get strict failures instead.
|
|
42
|
+
|
|
43
|
+
The default can be changed in `pyproject.toml`:
|
|
44
|
+
|
|
45
|
+
```toml
|
|
46
|
+
[tool.pytest.ini_options]
|
|
47
|
+
remaster-by-default = false
|
|
48
|
+
```
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
pytest_remaster/__init__.py,sha256=4SMDIcU8nsmnBdJSBCksm5B7-ddpRFfbwhZz-Bs0g2s,303
|
|
2
|
+
pytest_remaster/core.py,sha256=PmVl6w1fPSDpJII2pdPargCJewr5iMlpm2TdXoDwPcU,5063
|
|
3
|
+
pytest_remaster/plugin.py,sha256=XXgOavLi_07UAXBy3eHJn_fdmVrKD3ks6c9mnJ1vCPw,1737
|
|
4
|
+
pytest_remaster-0.0.0.dist-info/licenses/LICENSE,sha256=Vjs_JOq3WNrBenjiN_uYE7PHRbDMgeVRqh5doPioyLI,1073
|
|
5
|
+
pytest_remaster-0.0.0.dist-info/METADATA,sha256=Dc1SdRF7uYyWFJxpeIPmo7JrIcmA2KEUBMvL1c2KUmg,1790
|
|
6
|
+
pytest_remaster-0.0.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
7
|
+
pytest_remaster-0.0.0.dist-info/entry_points.txt,sha256=nDuTiytFZYTZJA_dqr_RaCP1kyQmq_ymuofhPBToibc,45
|
|
8
|
+
pytest_remaster-0.0.0.dist-info/top_level.txt,sha256=8KgXdYqLwtFgKc6G9Ip9F79Vl4wt3HK4mlmrOF00hTs,16
|
|
9
|
+
pytest_remaster-0.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pierre Sassoulas
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pytest_remaster
|