44
44
45
45
from pytest_mpl .summary .html import generate_summary_basic_html , generate_summary_html
46
46
47
- SUPPORTED_FORMATS = {'html' , 'json' , 'basic-html' }
47
+ DEFAULT_STYLE = "classic"
48
+ DEFAULT_TOLERANCE : float = 2
49
+ DEFAULT_BACKEND = "agg"
50
+
51
+ SUPPORTED_FORMATS = {"html" , "json" , "basic-html" }
48
52
49
53
SHAPE_MISMATCH_ERROR = """Error: Image dimensions did not match.
50
54
Expected shape: {expected_shape}
@@ -125,60 +129,71 @@ def pytest_report_header(config, startdir):
125
129
126
130
def pytest_addoption (parser ):
127
131
group = parser .getgroup ("matplotlib image comparison" )
128
- group .addoption ('--mpl' , action = 'store_true' ,
129
- help = "Enable comparison of matplotlib figures to reference files" )
130
- group .addoption ('--mpl-generate-path' ,
131
- help = "directory to generate reference images in, relative "
132
- "to location where py.test is run" , action = 'store' )
133
- group .addoption ('--mpl-generate-hash-library' ,
134
- help = "filepath to save a generated hash library, relative "
135
- "to location where py.test is run" , action = 'store' )
136
-
137
- baseline_path_help = (
132
+
133
+ msg = "Enable comparison of matplotlib figures to reference files"
134
+ group .addoption ("--mpl" , help = msg , action = "store_true" )
135
+
136
+ msg = "directory to generate reference images in, relative to location where py.test is run"
137
+ group .addoption ("--mpl-generate-path" , help = msg , action = "store" )
138
+
139
+ msg = "filepath to save a generated hash library, relative to location where py.test is run"
140
+ group .addoption ("--mpl-generate-hash-library" , help = msg , action = "store" )
141
+
142
+ msg = (
138
143
"directory containing baseline images, relative to "
139
144
"location where py.test is run unless --mpl-baseline-relative is given. "
140
145
"This can also be a URL or a set of comma-separated URLs (in case "
141
146
"mirrors are specified)"
142
147
)
143
- group .addoption ("--mpl-baseline-path" , help = baseline_path_help , action = "store" )
144
- parser .addini ("mpl-baseline-path" , help = baseline_path_help )
148
+ option = "mpl-baseline-path"
149
+ group .addoption (f"--{ option } " , help = msg , action = "store" )
150
+ parser .addini (option , help = msg )
145
151
146
- group .addoption ("--mpl-baseline-relative" , help = "interpret the baseline directory as "
147
- "relative to the test location." , action = "store_true" )
148
- mpl_hash_library_help = "json library of image hashes, relative to location where py.test is run"
149
- group .addoption ('--mpl-hash-library' , help = mpl_hash_library_help , action = 'store' )
150
- parser .addini ("mpl-hash-library" , help = mpl_hash_library_help )
152
+ msg = "interpret the baseline directory as relative to the test location."
153
+ group .addoption ("--mpl-baseline-relative" , help = msg , action = "store_true" )
151
154
152
- mpl_generate_summary_help = (
155
+ msg = "json library of image hashes, relative to location where py.test is run"
156
+ option = "mpl-hash-library"
157
+ group .addoption (f"--{ option } " , help = msg , action = "store" )
158
+ parser .addini (option , help = msg )
159
+
160
+ msg = (
153
161
"Generate a summary report of any failed tests"
154
162
", in --mpl-results-path. The type of the report should be "
155
163
"specified. Supported types are `html`, `json` and `basic-html`. "
156
164
"Multiple types can be specified separated by commas."
157
165
)
158
- group .addoption ("--mpl-generate-summary" , help = mpl_generate_summary_help , action = "store" )
159
- parser .addini ("mpl-generate-summary" , help = mpl_generate_summary_help )
166
+ option = "mpl-generate-summary"
167
+ group .addoption (f"--{ option } " , help = msg , action = "store" )
168
+ parser .addini (option , help = msg )
160
169
161
- results_path_help = "directory for test results, relative to location where py.test is run"
162
- group .addoption ('--mpl-results-path' , help = results_path_help , action = 'store' )
163
- parser .addini ('mpl-results-path' , help = results_path_help )
170
+ msg = "directory for test results, relative to location where py.test is run"
171
+ option = "mpl-results-path"
172
+ group .addoption (f"--{ option } " , help = msg , action = "store" )
173
+ parser .addini (option , help = msg )
164
174
165
- results_always_help = ("Always compare to baseline images and save result images, even for passing tests. "
166
- "This option is automatically applied when generating a HTML summary." )
167
- group .addoption ('--mpl-results-always' , action = 'store_true' ,
168
- help = results_always_help )
169
- parser .addini ('mpl-results-always' , help = results_always_help )
175
+ msg = (
176
+ "Always compare to baseline images and save result images, even for passing tests. "
177
+ "This option is automatically applied when generating a HTML summary."
178
+ )
179
+ option = "mpl-results-always"
180
+ group .addoption (f"--{ option } " , help = msg , action = "store_true" )
181
+ parser .addini (option , help = msg )
170
182
171
- use_full_test_name_help = "use fully qualified test name as the filename."
172
- group .addoption ("--mpl-use-full-test-name" , help = use_full_test_name_help , action = "store_true" )
173
- parser .addini ('mpl-use-full-test-name' , help = use_full_test_name_help , type = 'bool' )
183
+ msg = "use fully qualified test name as the filename."
184
+ option = "mpl-use-full-test-name"
185
+ group .addoption (f"--{ option } " , help = msg , action = "store_true" )
186
+ parser .addini (option , help = msg , type = "bool" )
174
187
175
- style_help = "default style to use for tests, unless specified in the mpl_image_compare decorator"
176
- group .addoption ('--mpl-default-style' , help = style_help , action = 'store' )
177
- parser .addini ('mpl-default-style' , help = style_help )
188
+ msg = "default style to use for tests, unless specified in the mpl_image_compare decorator"
189
+ option = "mpl-default-style"
190
+ group .addoption (f"--{ option } " , help = msg , action = "store" )
191
+ parser .addini (option , help = msg )
178
192
179
- tolerance_help = "default tolerance to use for tests, unless specified in the mpl_image_compare decorator"
180
- group .addoption ('--mpl-default-tolerance' , help = tolerance_help , action = 'store' )
181
- parser .addini ('mpl-default-tolerance' , help = tolerance_help )
193
+ msg = "default tolerance to use for tests, unless specified in the mpl_image_compare decorator"
194
+ option = "mpl-default-tolerance"
195
+ group .addoption (f"--{ option } " , help = msg , action = "store" )
196
+ parser .addini (option , help = msg )
182
197
183
198
msg = "default backend to use for tests, unless specified in the mpl_image_compare decorator"
184
199
option = "mpl-default-backend"
@@ -188,36 +203,40 @@ def pytest_addoption(parser):
188
203
189
204
def pytest_configure (config ):
190
205
191
- config .addinivalue_line ('markers' ,
192
- "mpl_image_compare: Compares matplotlib figures "
193
- "against a baseline image" )
206
+ config .addinivalue_line (
207
+ "markers" ,
208
+ "mpl_image_compare: Compares matplotlib figures against a baseline image" ,
209
+ )
210
+
211
+ if (
212
+ config .getoption ("--mpl" )
213
+ or config .getoption ("--mpl-generate-path" ) is not None
214
+ or config .getoption ("--mpl-generate-hash-library" ) is not None
215
+ ):
194
216
195
- if (config .getoption ("--mpl" ) or
196
- config .getoption ("--mpl-generate-path" ) is not None or
197
- config .getoption ("--mpl-generate-hash-library" ) is not None ):
217
+ def get_cli_or_ini (name , default = None ):
218
+ return config .getoption (f"--{ name } " ) or config .getini (name ) or default
198
219
199
- baseline_dir = (config .getoption ("--mpl-baseline-path" ) or
200
- config .getini ("mpl-baseline-path" ))
201
220
generate_dir = config .getoption ("--mpl-generate-path" )
202
221
generate_hash_lib = config .getoption ("--mpl-generate-hash-library" )
203
- results_dir = config .getoption ("--mpl-results-path" ) or config .getini ("mpl-results-path" )
204
- hash_library = config .getoption ("--mpl-hash-library" ) or config .getini ("mpl-hash-library" )
205
- _hash_library_from_cli = bool (config .getoption ("--mpl-hash-library" )) # for backwards compatibility
206
- generate_summary = (config .getoption ("--mpl-generate-summary" ) or
207
- config .getini ("mpl-generate-summary" ))
208
- results_always = (config .getoption ("--mpl-results-always" ) or
209
- config .getini ("mpl-results-always" ))
210
- use_full_test_name = (config .getoption ("--mpl-use-full-test-name" ) or
211
- config .getini ("mpl-use-full-test-name" ))
212
222
223
+ baseline_dir = get_cli_or_ini ("mpl-baseline-path" )
213
224
if config .getoption ("--mpl-baseline-relative" ):
214
225
baseline_relative_dir = config .getoption ("--mpl-baseline-path" )
215
226
else :
216
227
baseline_relative_dir = None
228
+ use_full_test_name = get_cli_or_ini ("mpl-use-full-test-name" )
229
+
230
+ hash_library = get_cli_or_ini ("mpl-hash-library" )
231
+ _hash_library_from_cli = bool (config .getoption ("--mpl-hash-library" )) # for backwards compatibility
217
232
218
- # Note that results_dir is an empty string if not specified
219
- if not results_dir :
220
- results_dir = None
233
+ default_tolerance = float (get_cli_or_ini ("mpl-default-tolerance" , DEFAULT_TOLERANCE ))
234
+ default_style = get_cli_or_ini ("mpl-default-style" , DEFAULT_STYLE )
235
+ default_backend = get_cli_or_ini ("mpl-default-backend" , DEFAULT_BACKEND )
236
+
237
+ results_dir = get_cli_or_ini ("mpl-results-path" )
238
+ results_always = get_cli_or_ini ("mpl-results-always" )
239
+ generate_summary = get_cli_or_ini ("mpl-generate-summary" )
221
240
222
241
if generate_dir is not None :
223
242
if baseline_dir is not None :
@@ -234,35 +253,25 @@ def pytest_configure(config):
234
253
if not _hash_library_from_cli :
235
254
hash_library = os .path .abspath (hash_library )
236
255
237
- default_style = (config .getoption ("--mpl-default-style" ) or
238
- config .getini ("mpl-default-style" ) or
239
- "classic" )
240
-
241
- default_tolerance = float (config .getoption ("--mpl-default-tolerance" ) or
242
- config .getini ("mpl-default-tolerance" ) or
243
- "2" )
244
-
245
- default_backend = (config .getoption ("--mpl-default-backend" ) or
246
- config .getini ("mpl-default-backend" ) or
247
- "agg" )
248
-
249
- config .pluginmanager .register (ImageComparison (config ,
250
- baseline_dir = baseline_dir ,
251
- baseline_relative_dir = baseline_relative_dir ,
252
- generate_dir = generate_dir ,
253
- results_dir = results_dir ,
254
- hash_library = hash_library ,
255
- generate_hash_library = generate_hash_lib ,
256
- generate_summary = generate_summary ,
257
- results_always = results_always ,
258
- use_full_test_name = use_full_test_name ,
259
- default_style = default_style ,
260
- default_tolerance = default_tolerance ,
261
- default_backend = default_backend ,
262
- _hash_library_from_cli = _hash_library_from_cli ))
256
+ plugin = ImageComparison (
257
+ config ,
258
+ baseline_dir = baseline_dir ,
259
+ baseline_relative_dir = baseline_relative_dir ,
260
+ generate_dir = generate_dir ,
261
+ results_dir = results_dir ,
262
+ hash_library = hash_library ,
263
+ generate_hash_library = generate_hash_lib ,
264
+ generate_summary = generate_summary ,
265
+ results_always = results_always ,
266
+ use_full_test_name = use_full_test_name ,
267
+ default_style = default_style ,
268
+ default_tolerance = default_tolerance ,
269
+ default_backend = default_backend ,
270
+ _hash_library_from_cli = _hash_library_from_cli ,
271
+ )
272
+ config .pluginmanager .register (plugin )
263
273
264
274
else :
265
-
266
275
config .pluginmanager .register (FigureCloser (config ))
267
276
268
277
@@ -305,23 +314,23 @@ def path_is_not_none(apath):
305
314
306
315
307
316
class ImageComparison :
308
-
309
- def __init__ ( self ,
310
- config ,
311
- baseline_dir = None ,
312
- baseline_relative_dir = None ,
313
- generate_dir = None ,
314
- results_dir = None ,
315
- hash_library = None ,
316
- generate_hash_library = None ,
317
- generate_summary = None ,
318
- results_always = False ,
319
- use_full_test_name = False ,
320
- default_style = 'classic' ,
321
- default_tolerance = 2 ,
322
- default_backend = 'agg' ,
323
- _hash_library_from_cli = False , # for backwards compatibility
324
- ):
317
+ def __init__ (
318
+ self ,
319
+ config ,
320
+ baseline_dir = None ,
321
+ baseline_relative_dir = None ,
322
+ generate_dir = None ,
323
+ results_dir = None ,
324
+ hash_library = None ,
325
+ generate_hash_library = None ,
326
+ generate_summary = None ,
327
+ results_always = False ,
328
+ use_full_test_name = False ,
329
+ default_style = DEFAULT_STYLE ,
330
+ default_tolerance = DEFAULT_TOLERANCE ,
331
+ default_backend = DEFAULT_BACKEND ,
332
+ _hash_library_from_cli = False , # for backwards compatibility
333
+ ):
325
334
self .config = config
326
335
self .baseline_dir = baseline_dir
327
336
self .baseline_relative_dir = path_is_not_none (baseline_relative_dir )
0 commit comments