Skip to content

Commit 113acba

Browse files
committed
Refactor image_status and hash_status to plugin.py
Include specific results from the image and hash test in the JSON summaries instead of infering them from RMS etc.
1 parent 6388f6f commit 113acba

14 files changed

+349
-27
lines changed

pytest_mpl/plugin.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ def compare_image_to_baseline(self, item, fig, result_dir, summary=None):
442442

443443
if not os.path.exists(baseline_image_ref):
444444
summary['status'] = 'failed'
445+
summary['image_status'] = 'missing'
445446
error_message = ("Image file not found for comparison test in: \n\t"
446447
f"{self.get_baseline_directory(item)}\n"
447448
"(This is expected for new tests.)\n"
@@ -463,6 +464,7 @@ def compare_image_to_baseline(self, item, fig, result_dir, summary=None):
463464
actual_shape = imread(str(test_image)).shape[:2]
464465
if expected_shape != actual_shape:
465466
summary['status'] = 'failed'
467+
summary['image_status'] = 'diff'
466468
error_message = SHAPE_MISMATCH_ERROR.format(expected_path=baseline_image,
467469
expected_shape=expected_shape,
468470
actual_path=test_image,
@@ -474,10 +476,12 @@ def compare_image_to_baseline(self, item, fig, result_dir, summary=None):
474476
summary['tolerance'] = tolerance
475477
if results is None:
476478
summary['status'] = 'passed'
479+
summary['image_status'] = 'match'
477480
summary['status_msg'] = 'Image comparison passed.'
478481
return None
479482
else:
480483
summary['status'] = 'failed'
484+
summary['image_status'] = 'diff'
481485
summary['rms'] = results['rms']
482486
diff_image = (result_dir / 'result-failed-diff.png').absolute()
483487
summary['diff_image'] = diff_image.relative_to(self.results_dir).as_posix()
@@ -523,14 +527,17 @@ def compare_image_to_hash_library(self, item, fig, result_dir, summary=None):
523527

524528
if baseline_hash is None: # hash-missing
525529
summary['status'] = 'failed'
530+
summary['hash_status'] = 'missing'
526531
summary['status_msg'] = (f"Hash for test '{hash_name}' not found in {hash_library_filename}. "
527532
f"Generated hash is {test_hash}.")
528533
elif test_hash == baseline_hash: # hash-match
529534
hash_comparison_pass = True
530535
summary['status'] = 'passed'
536+
summary['hash_status'] = 'match'
531537
summary['status_msg'] = 'Test hash matches baseline hash.'
532538
else: # hash-diff
533539
summary['status'] = 'failed'
540+
summary['hash_status'] = 'diff'
534541
summary['status_msg'] = (f"Hash {test_hash} doesn't match hash "
535542
f"{baseline_hash} in library "
536543
f"{hash_library_filename} for test {hash_name}.")
@@ -555,7 +562,8 @@ def compare_image_to_hash_library(self, item, fig, result_dir, summary=None):
555562
except Exception as baseline_error: # Append to test error later
556563
baseline_comparison = str(baseline_error)
557564
else: # Update main summary
558-
for k in ['baseline_image', 'diff_image', 'rms', 'tolerance', 'result_image']:
565+
for k in ['image_status', 'baseline_image', 'diff_image',
566+
'rms', 'tolerance', 'result_image']:
559567
summary[k] = summary[k] or baseline_summary.get(k)
560568

561569
# Append the log from image comparison
@@ -612,6 +620,8 @@ def item_function_wrapper(*args, **kwargs):
612620

613621
summary = {
614622
'status': None,
623+
'image_status': None,
624+
'hash_status': None,
615625
'status_msg': None,
616626
'baseline_image': None,
617627
'diff_image': None,
@@ -626,6 +636,7 @@ def item_function_wrapper(*args, **kwargs):
626636
# reference images or simply running the test.
627637
if self.generate_dir is not None:
628638
summary['status'] = 'skipped'
639+
summary['image_status'] = 'generated'
629640
summary['status_msg'] = 'Skipped test, since generating image.'
630641
generate_image = self.generate_baseline_image(item, fig)
631642
if self.results_always: # Make baseline image available in HTML
@@ -635,6 +646,7 @@ def item_function_wrapper(*args, **kwargs):
635646
result_image.relative_to(self.results_dir).as_posix()
636647

637648
if self.generate_hash_library is not None:
649+
summary['hash_status'] = 'generated'
638650
image_hash = self.generate_image_hash(item, fig)
639651
self._generated_hash_library[test_name] = image_hash
640652
summary['baseline_hash'] = image_hash

pytest_mpl/summary/html.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -107,32 +107,6 @@ def __init__(self, name, item, id, warn_missing):
107107
('hash', self.hash_status),
108108
]]
109109

110-
@cached_property
111-
def image_status(self):
112-
"""Status of the image comparison test."""
113-
if self.rms is None and self.tolerance is not None:
114-
return 'match'
115-
elif self.rms is not None:
116-
return 'diff'
117-
elif self.baseline_image is None:
118-
return 'missing'
119-
else:
120-
return 'generated'
121-
122-
@cached_property
123-
def hash_status(self):
124-
"""Status of the hash comparison test."""
125-
if self.baseline_hash is not None or self.result_hash is not None:
126-
if self.baseline_hash is None:
127-
return 'missing'
128-
elif self.result_hash is None:
129-
return 'generated'
130-
elif self.baseline_hash == self.result_hash:
131-
return 'match'
132-
else:
133-
return 'diff'
134-
return None
135-
136110
@cached_property
137111
def indexes(self):
138112
"""Dictionary with strings optimized for sorting."""

tests/subtests/summaries/test_default.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"subtests.subtest.test_hmatch_imatch": {
33
"status": "passed",
4+
"image_status": "match",
5+
"hash_status": null,
46
"status_msg": "Image comparison passed.",
57
"baseline_image": null,
68
"diff_image": null,
@@ -12,6 +14,8 @@
1214
},
1315
"subtests.subtest.test_hmatch_idiff": {
1416
"status": "failed",
17+
"image_status": "diff",
18+
"hash_status": null,
1519
"status_msg": "REGEX:Error: Image files did not match\\.\n RMS Value: 11\\.1[0-9]*\n Expected: \n .*baseline\\.png\n Actual: \n .*result\\.png\n Difference:\n .*result-failed-diff\\.png\n Tolerance: \n 2",
1620
"baseline_image": "subtests.subtest.test_hmatch_idiff/baseline.png",
1721
"diff_image": "subtests.subtest.test_hmatch_idiff/result-failed-diff.png",
@@ -23,6 +27,8 @@
2327
},
2428
"subtests.subtest.test_hmatch_imissing": {
2529
"status": "failed",
30+
"image_status": "missing",
31+
"hash_status": null,
2632
"status_msg": "REGEX:Image file not found for comparison test in: \n\t.*\n\\(This is expected for new tests\\.\\)\nGenerated Image: \n\t.*result\\.png",
2733
"baseline_image": null,
2834
"diff_image": null,
@@ -34,6 +40,8 @@
3440
},
3541
"subtests.subtest.test_hdiff_imatch": {
3642
"status": "passed",
43+
"image_status": "match",
44+
"hash_status": null,
3745
"status_msg": "Image comparison passed.",
3846
"baseline_image": null,
3947
"diff_image": null,
@@ -45,6 +53,8 @@
4553
},
4654
"subtests.subtest.test_hdiff_idiff": {
4755
"status": "failed",
56+
"image_status": "diff",
57+
"hash_status": null,
4858
"status_msg": "REGEX:Error: Image files did not match\\.\n RMS Value: 11\\.1[0-9]*\n Expected: \n .*baseline\\.png\n Actual: \n .*result\\.png\n Difference:\n .*result-failed-diff\\.png\n Tolerance: \n 2",
4959
"baseline_image": "subtests.subtest.test_hdiff_idiff/baseline.png",
5060
"diff_image": "subtests.subtest.test_hdiff_idiff/result-failed-diff.png",
@@ -56,6 +66,8 @@
5666
},
5767
"subtests.subtest.test_hdiff_imissing": {
5868
"status": "failed",
69+
"image_status": "missing",
70+
"hash_status": null,
5971
"status_msg": "REGEX:Image file not found for comparison test in: \n\t.*\n\\(This is expected for new tests\\.\\)\nGenerated Image: \n\t.*result\\.png",
6072
"baseline_image": null,
6173
"diff_image": null,
@@ -67,6 +79,8 @@
6779
},
6880
"subtests.subtest.test_hmissing_imatch": {
6981
"status": "passed",
82+
"image_status": "match",
83+
"hash_status": null,
7084
"status_msg": "Image comparison passed.",
7185
"baseline_image": null,
7286
"diff_image": null,
@@ -78,6 +92,8 @@
7892
},
7993
"subtests.subtest.test_hmissing_idiff": {
8094
"status": "failed",
95+
"image_status": "diff",
96+
"hash_status": null,
8197
"status_msg": "REGEX:Error: Image files did not match\\.\n RMS Value: 12\\.1[0-9]*\n Expected: \n .*baseline\\.png\n Actual: \n .*result\\.png\n Difference:\n .*result-failed-diff\\.png\n Tolerance: \n 2",
8298
"baseline_image": "subtests.subtest.test_hmissing_idiff/baseline.png",
8399
"diff_image": "subtests.subtest.test_hmissing_idiff/result-failed-diff.png",
@@ -89,6 +105,8 @@
89105
},
90106
"subtests.subtest.test_hmissing_imissing": {
91107
"status": "failed",
108+
"image_status": "missing",
109+
"hash_status": null,
92110
"status_msg": "REGEX:Image file not found for comparison test in: \n\t.*\n\\(This is expected for new tests\\.\\)\nGenerated Image: \n\t.*result\\.png",
93111
"baseline_image": null,
94112
"diff_image": null,
@@ -100,6 +118,8 @@
100118
},
101119
"subtests.subtest.test_hdiff_imatch_tolerance": {
102120
"status": "passed",
121+
"image_status": "match",
122+
"hash_status": null,
103123
"status_msg": "Image comparison passed.",
104124
"baseline_image": null,
105125
"diff_image": null,
@@ -111,6 +131,8 @@
111131
},
112132
"subtests.subtest.test_hdiff_idiff_tolerance": {
113133
"status": "failed",
134+
"image_status": "diff",
135+
"hash_status": null,
114136
"status_msg": "REGEX:Error: Image files did not match\\.\n RMS Value: 29\\.2[0-9]*\n Expected: \n .*baseline\\.png\n Actual: \n .*result\\.png\n Difference:\n .*result-failed-diff\\.png\n Tolerance: \n 3",
115137
"baseline_image": "subtests.subtest.test_hdiff_idiff_tolerance/baseline.png",
116138
"diff_image": "subtests.subtest.test_hdiff_idiff_tolerance/result-failed-diff.png",
@@ -122,6 +144,8 @@
122144
},
123145
"subtests.subtest.test_hdiff_imatch_savefig": {
124146
"status": "passed",
147+
"image_status": "match",
148+
"hash_status": null,
125149
"status_msg": "Image comparison passed.",
126150
"baseline_image": null,
127151
"diff_image": null,
@@ -133,6 +157,8 @@
133157
},
134158
"subtests.subtest.test_hdiff_imatch_style": {
135159
"status": "passed",
160+
"image_status": "match",
161+
"hash_status": null,
136162
"status_msg": "Image comparison passed.",
137163
"baseline_image": null,
138164
"diff_image": null,
@@ -144,6 +170,8 @@
144170
},
145171
"subtests.subtest.test_hdiff_imatch_removetext": {
146172
"status": "passed",
173+
"image_status": "match",
174+
"hash_status": null,
147175
"status_msg": "Image comparison passed.",
148176
"baseline_image": null,
149177
"diff_image": null,

tests/subtests/summaries/test_generate.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"subtests.subtest.test_hmatch_imatch": {
33
"status": "skipped",
4+
"image_status": "generated",
5+
"hash_status": "generated",
46
"status_msg": "Skipped test, since generating image.",
57
"baseline_image": null,
68
"diff_image": null,
@@ -12,6 +14,8 @@
1214
},
1315
"subtests.subtest.test_hmatch_idiff": {
1416
"status": "skipped",
17+
"image_status": "generated",
18+
"hash_status": "generated",
1519
"status_msg": "Skipped test, since generating image.",
1620
"baseline_image": null,
1721
"diff_image": null,
@@ -23,6 +27,8 @@
2327
},
2428
"subtests.subtest.test_hmatch_imissing": {
2529
"status": "skipped",
30+
"image_status": "generated",
31+
"hash_status": "generated",
2632
"status_msg": "Skipped test, since generating image.",
2733
"baseline_image": null,
2834
"diff_image": null,
@@ -34,6 +40,8 @@
3440
},
3541
"subtests.subtest.test_hdiff_imatch": {
3642
"status": "skipped",
43+
"image_status": "generated",
44+
"hash_status": "generated",
3745
"status_msg": "Skipped test, since generating image.",
3846
"baseline_image": null,
3947
"diff_image": null,
@@ -45,6 +53,8 @@
4553
},
4654
"subtests.subtest.test_hdiff_idiff": {
4755
"status": "skipped",
56+
"image_status": "generated",
57+
"hash_status": "generated",
4858
"status_msg": "Skipped test, since generating image.",
4959
"baseline_image": null,
5060
"diff_image": null,
@@ -56,6 +66,8 @@
5666
},
5767
"subtests.subtest.test_hdiff_imissing": {
5868
"status": "skipped",
69+
"image_status": "generated",
70+
"hash_status": "generated",
5971
"status_msg": "Skipped test, since generating image.",
6072
"baseline_image": null,
6173
"diff_image": null,
@@ -67,6 +79,8 @@
6779
},
6880
"subtests.subtest.test_hmissing_imatch": {
6981
"status": "skipped",
82+
"image_status": "generated",
83+
"hash_status": "generated",
7084
"status_msg": "Skipped test, since generating image.",
7185
"baseline_image": null,
7286
"diff_image": null,
@@ -78,6 +92,8 @@
7892
},
7993
"subtests.subtest.test_hmissing_idiff": {
8094
"status": "skipped",
95+
"image_status": "generated",
96+
"hash_status": "generated",
8197
"status_msg": "Skipped test, since generating image.",
8298
"baseline_image": null,
8399
"diff_image": null,
@@ -89,6 +105,8 @@
89105
},
90106
"subtests.subtest.test_hmissing_imissing": {
91107
"status": "skipped",
108+
"image_status": "generated",
109+
"hash_status": "generated",
92110
"status_msg": "Skipped test, since generating image.",
93111
"baseline_image": null,
94112
"diff_image": null,
@@ -100,6 +118,8 @@
100118
},
101119
"subtests.subtest.test_hdiff_imatch_tolerance": {
102120
"status": "skipped",
121+
"image_status": "generated",
122+
"hash_status": "generated",
103123
"status_msg": "Skipped test, since generating image.",
104124
"baseline_image": null,
105125
"diff_image": null,
@@ -111,6 +131,8 @@
111131
},
112132
"subtests.subtest.test_hdiff_idiff_tolerance": {
113133
"status": "skipped",
134+
"image_status": "generated",
135+
"hash_status": "generated",
114136
"status_msg": "Skipped test, since generating image.",
115137
"baseline_image": null,
116138
"diff_image": null,
@@ -122,6 +144,8 @@
122144
},
123145
"subtests.subtest.test_hdiff_imatch_savefig": {
124146
"status": "skipped",
147+
"image_status": "generated",
148+
"hash_status": "generated",
125149
"status_msg": "Skipped test, since generating image.",
126150
"baseline_image": null,
127151
"diff_image": null,
@@ -133,6 +157,8 @@
133157
},
134158
"subtests.subtest.test_hdiff_imatch_style": {
135159
"status": "skipped",
160+
"image_status": "generated",
161+
"hash_status": "generated",
136162
"status_msg": "Skipped test, since generating image.",
137163
"baseline_image": null,
138164
"diff_image": null,
@@ -144,6 +170,8 @@
144170
},
145171
"subtests.subtest.test_hdiff_imatch_removetext": {
146172
"status": "skipped",
173+
"image_status": "generated",
174+
"hash_status": "generated",
147175
"status_msg": "Skipped test, since generating image.",
148176
"baseline_image": null,
149177
"diff_image": null,

0 commit comments

Comments
 (0)