Debian Codegraph @

src/pytest_regressions/plugin.py

raw symbols SCIP
1 from pathlib import Path
2 from typing import Any
3 from typing import TYPE_CHECKING
4
5 import pytest
6
7 if TYPE_CHECKING:
8 from .data_regression import DataRegressionFixture
9 from .dataframe_regression import DataFrameRegressionFixture
10 from .ndarrays_regression import NDArraysRegressionFixture
11 from .file_regression import FileRegressionFixture
12 from .num_regression import NumericRegressionFixture
13 from .image_regression import ImageRegressionFixture
14
15
16 def pytest_addoption(parser: Any) -> None:
17 group = parser.getgroup("regressions")
18 group.addoption(
19 "--force-regen",
20 action="store_true",
21 default=False,
22 help="Regenerate regression data files, failing tests with different data.",
23 )
24 group.addoption(
25 "--regen-all",
26 action="store_true",
27 default=False,
28 help="Regenerate all files, letting tests pass (use to regenerate everything in one run).",
29 )
30 group.addoption(
31 "--with-test-class-names",
32 action="store_true",
33 default=False,
34 help="Do not ignore the names of the test classes when composing the name of the regression data files.",
35 )
36
37
38 @pytest.fixture
39 def data_regression(
40 datadir: Path, original_datadir: Path, request: pytest.FixtureRequest
41 ) -> "DataRegressionFixture":
42 """
43 Fixture used to test arbitrary data against known versions previously
44 recorded by this same fixture. Useful to test 3rd party APIs or where testing directly
45 generated data from other sources.
46
47 Create a dict in your test containing a arbitrary data you want to test, and
48 call the `Check` function. The first time it will fail but will generate a file in your
49 data directory.
50
51 Subsequent runs against the same data will now compare against the generated file and pass
52 if the dicts are equal, or fail showing a diff otherwise. To regenerate data for failed tests,
53 either set `force_regen` attribute to True or pass the `--force-regen` flag to pytest
54 which will regenerate the data files for all failed tests. Make sure to diff the files to ensure
55 the new changes are expected and not regressions.
56
57 The dict may be anything serializable by the `yaml` library.
58 """
59 from .data_regression import DataRegressionFixture
60
61 return DataRegressionFixture(datadir, original_datadir, request)
62
63
64 @pytest.fixture
65 def dataframe_regression(
66 datadir: Path, original_datadir: Path, request: pytest.FixtureRequest
67 ) -> "DataFrameRegressionFixture":
68 """
69 Example usage:
70
71 def testSomeData(dataframe_regression):
72 dataframe_regression.check(
73 pandas.DataFrame.from_dict(
74 {
75 'U_gas': U[0],
76 'U_liquid': U[1],
77 'gas_vol_frac [-]': α[0],
78 'liquid_vol_frac [-]': α[1],
79 'P': Pa_to_bar(P),
80 }
81 ),
82 default_tolerance=dict(atol=1e-8, rtol=1e-8)
83 )
84 """
85 from .dataframe_regression import DataFrameRegressionFixture
86
87 return DataFrameRegressionFixture(datadir, original_datadir, request)
88
89
90 @pytest.fixture
91 def ndarrays_regression(
92 datadir: Path, original_datadir: Path, request: pytest.FixtureRequest
93 ) -> "NDArraysRegressionFixture":
94 """
95 Similar to num_regression, but supports numpy arrays with arbitrary shape. The
96 dictionary is stored as an NPZ file. The values of the dictionary must be accepted
97 by ``np.asarray``.
98
99 Example::
100
101 def test_some_data(ndarrays_regression):
102 points, values = some_function()
103 ndarrays_regression.check(
104 {
105 'points': points, # array with shape (100, 3)
106 'values': values, # array with shape (100,)
107 },
108 default_tolerance=dict(atol=1e-8, rtol=1e-8)
109 )
110 """
111 from .ndarrays_regression import NDArraysRegressionFixture
112
113 return NDArraysRegressionFixture(datadir, original_datadir, request)
114
115
116 @pytest.fixture
117 def file_regression(
118 datadir: Path, original_datadir: Path, request: pytest.FixtureRequest
119 ) -> "FileRegressionFixture":
120 """
121 Very similar to `data_regression`, but instead of saving data to YAML file it saves to an
122 arbitrary format.
123
124 Useful when used to compare contents of files of specific format (like documents, for instance).
125 """
126 from .file_regression import FileRegressionFixture
127
128 return FileRegressionFixture(datadir, original_datadir, request)
129
130
131 @pytest.fixture
132 def num_regression(
133 datadir: Path, original_datadir: Path, request: pytest.FixtureRequest
134 ) -> "NumericRegressionFixture":
135 """
136 Example usage:
137
138 def testSomeData(num_regression):
139 num_regression.check(
140 {
141 'U_gas': U[0],
142 'U_liquid': U[1],
143 'gas_vol_frac [-]': α[0],
144 'liquid_vol_frac [-]': α[1],
145 'P': Pa_to_bar(P),
146 },
147 data_index=positions,
148 default_tolerance=dict(atol=1e-8, rtol=1e-8)
149 )
150 """
151 from .num_regression import NumericRegressionFixture
152
153 return NumericRegressionFixture(datadir, original_datadir, request)
154
155
156 @pytest.fixture
157 def image_regression(
158 datadir: Path, original_datadir: Path, request: pytest.FixtureRequest
159 ) -> "ImageRegressionFixture":
160 """
161 Regression checks for images, accounting for small differences between them.
162
163 Example:
164 def test_plots(datadir, image_regression):
165 path = generate_plot(datadir / 'plot.png')
166 image_regression.check(path.read_bytes(), diff_threshold=2.5) # 2.5%
167 """
168 from .image_regression import ImageRegressionFixture
169
170 return ImageRegressionFixture(datadir, original_datadir, request)