@@ -109,6 +109,9 @@ def pytest_addoption(parser):
109
109
group .addoption ('--mpl-hash-library' ,
110
110
help = "json library of image hashes, relative to "
111
111
"location where py.test is run" , action = 'store' )
112
+ group .addoption ('--mpl-generate-summary' , action = 'store_true' ,
113
+ help = "Generate a summary HTML report of any failed tests"
114
+ ", in --mpl-results-path" )
112
115
113
116
results_path_help = "directory for test results, relative to location where py.test is run"
114
117
group .addoption ('--mpl-results-path' , help = results_path_help , action = 'store' )
@@ -130,6 +133,7 @@ def pytest_configure(config):
130
133
generate_hash_lib = config .getoption ("--mpl-generate-hash-library" )
131
134
results_dir = config .getoption ("--mpl-results-path" ) or config .getini ("mpl-results-path" )
132
135
hash_library = config .getoption ("--mpl-hash-library" )
136
+ generate_summary = config .getoption ("--mpl-generate-summary" )
133
137
134
138
if config .getoption ("--mpl-baseline-relative" ):
135
139
baseline_relative_dir = config .getoption ("--mpl-baseline-path" )
@@ -159,7 +163,8 @@ def pytest_configure(config):
159
163
generate_dir = generate_dir ,
160
164
results_dir = results_dir ,
161
165
hash_library = hash_library ,
162
- generate_hash_library = generate_hash_lib ))
166
+ generate_hash_library = generate_hash_lib ,
167
+ generate_summary = generate_summary ))
163
168
164
169
else :
165
170
@@ -215,7 +220,8 @@ def __init__(self,
215
220
generate_dir = None ,
216
221
results_dir = None ,
217
222
hash_library = None ,
218
- generate_hash_library = None
223
+ generate_hash_library = None ,
224
+ generate_summary = False
219
225
):
220
226
self .config = config
221
227
self .baseline_dir = baseline_dir
@@ -224,8 +230,12 @@ def __init__(self,
224
230
self .results_dir = path_is_not_none (results_dir )
225
231
self .hash_library = path_is_not_none (hash_library )
226
232
self .generate_hash_library = path_is_not_none (generate_hash_library )
227
- if self .results_dir and not self .results_dir .exists ():
228
- self .results_dir .mkdir ()
233
+ self .generate_summary = bool (generate_summary )
234
+
235
+ # Generate the containing dir for all test results
236
+ if not self .results_dir :
237
+ self .results_dir = Path (tempfile .mkdtemp (dir = self .results_dir ))
238
+ self .results_dir .mkdir (parents = True , exist_ok = True )
229
239
230
240
# We need global state to store all the hashes generated over the run
231
241
self ._generated_hash_library = {}
@@ -251,11 +261,20 @@ def generate_filename(self, item):
251
261
252
262
return filename
253
263
254
- def make_results_dir (self , item ):
264
+ def generate_test_name (self , item ):
265
+ """
266
+ Generate a unique name for the hash for this test.
267
+ """
268
+ return f"{ item .module .__name__ } .{ item .name } "
269
+
270
+ def make_test_results_dir (self , item ):
255
271
"""
256
272
Generate the directory to put the results in.
257
273
"""
258
- return Path (tempfile .mkdtemp (dir = self .results_dir ))
274
+ test_name = self .generate_test_name (item )
275
+ results_dir = self .results_dir / test_name
276
+ results_dir .mkdir (exist_ok = True , parents = True )
277
+ return results_dir
259
278
260
279
def baseline_directory_specified (self , item ):
261
280
"""
@@ -328,12 +347,6 @@ def generate_baseline_image(self, item, fig):
328
347
close_mpl_figure (fig )
329
348
pytest .skip ("Skipping test, since generating image" )
330
349
331
- def generate_hash_name (self , item ):
332
- """
333
- Generate a unique name for the hash for this test.
334
- """
335
- return f"{ item .module .__name__ } .{ item .name } "
336
-
337
350
def generate_image_hash (self , item , fig ):
338
351
"""
339
352
For a `matplotlib.figure.Figure`, returns the SHA256 hash as a hexadecimal
@@ -365,7 +378,7 @@ def compare_image_to_baseline(self, item, fig, result_dir):
365
378
366
379
baseline_image_ref = self .obtain_baseline_image (item , result_dir )
367
380
368
- test_image = (result_dir / self . generate_filename ( item ) ).absolute ()
381
+ test_image = (result_dir / "result.png" ).absolute ()
369
382
fig .savefig (str (test_image ), ** savefig_kwargs )
370
383
371
384
if not os .path .exists (baseline_image_ref ):
@@ -377,7 +390,7 @@ def compare_image_to_baseline(self, item, fig, result_dir):
377
390
378
391
# distutils may put the baseline images in non-accessible places,
379
392
# copy to our tmpdir to be sure to keep them in case of failure
380
- baseline_image = (result_dir / f "baseline- { self . generate_filename ( item ) } " ).absolute ()
393
+ baseline_image = (result_dir / "baseline.png " ).absolute ()
381
394
shutil .copyfile (baseline_image_ref , baseline_image )
382
395
383
396
# Compare image size ourselves since the Matplotlib
@@ -407,7 +420,7 @@ def compare_image_to_hash_library(self, item, fig, result_dir):
407
420
pytest .fail (f"Can't find hash library at path { hash_library_filename } " )
408
421
409
422
hash_library = self .load_hash_library (hash_library_filename )
410
- hash_name = self .generate_hash_name (item )
423
+ hash_name = self .generate_test_name (item )
411
424
412
425
if hash_name not in hash_library :
413
426
return f"Hash for test '{ hash_name } ' not found in { hash_library_filename } ."
@@ -489,12 +502,12 @@ def item_function_wrapper(*args, **kwargs):
489
502
self .generate_baseline_image (item , fig )
490
503
491
504
if self .generate_hash_library is not None :
492
- hash_name = self .generate_hash_name (item )
505
+ hash_name = self .generate_test_name (item )
493
506
self ._generated_hash_library [hash_name ] = self .generate_image_hash (item , fig )
494
507
495
508
# Only test figures if we are not generating hashes or images
496
509
if self .generate_dir is None and self .generate_hash_library is None :
497
- result_dir = self .make_results_dir (item )
510
+ result_dir = self .make_test_results_dir (item )
498
511
499
512
# Compare to hash library
500
513
if self .hash_library or compare .kwargs .get ('hash_library' , None ):
@@ -507,7 +520,8 @@ def item_function_wrapper(*args, **kwargs):
507
520
close_mpl_figure (fig )
508
521
509
522
if msg is None :
510
- shutil .rmtree (result_dir )
523
+ pass
524
+ # shutil.rmtree(result_dir)
511
525
else :
512
526
pytest .fail (msg , pytrace = False )
513
527
@@ -528,6 +542,9 @@ def pytest_unconfigure(self, config):
528
542
with open (hash_library_path , "w" ) as fp :
529
543
json .dump (self ._generated_hash_library , fp , indent = 2 )
530
544
545
+ if self .generate_summary :
546
+ breakpoint ()
547
+
531
548
532
549
class FigureCloser (object ):
533
550
"""
0 commit comments