Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 188a08d

Browse files
committed
Fix bugs and refactor compare_image_to_hash_library
1 parent a2fc28b commit 188a08d

File tree

1 file changed

+37
-66
lines changed

1 file changed

+37
-66
lines changed

pytest_mpl/plugin.py

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,7 @@ def load_hash_library(self, library_path):
489489
return json.load(fp)
490490

491491
def compare_image_to_hash_library(self, item, fig, result_dir, summary=None):
492-
new_test = False
493492
hash_comparison_pass = False
494-
baseline_image_path = None
495493
if summary is None:
496494
summary = {}
497495

@@ -506,85 +504,58 @@ def compare_image_to_hash_library(self, item, fig, result_dir, summary=None):
506504

507505
hash_library = self.load_hash_library(hash_library_filename)
508506
hash_name = self.generate_test_name(item)
507+
baseline_hash = hash_library.get(hash_name, None)
508+
summary['baseline_hash'] = baseline_hash
509509

510510
test_hash = self.generate_image_hash(item, fig)
511511
summary['result_hash'] = test_hash
512512

513-
if hash_name not in hash_library:
514-
new_test = True
513+
if baseline_hash is None: # hash-missing
515514
summary['status'] = 'failed'
516-
error_message = (f"Hash for test '{hash_name}' not found in {hash_library_filename}. "
517-
f"Generated hash is {test_hash}.")
518-
summary['status_msg'] = error_message
519-
else:
520-
summary['baseline_hash'] = hash_library[hash_name]
515+
summary['status_msg'] = (f"Hash for test '{hash_name}' not found in {hash_library_filename}. "
516+
f"Generated hash is {test_hash}.")
517+
elif test_hash == baseline_hash: # hash-match
518+
hash_comparison_pass = True
519+
summary['status'] = 'passed'
520+
summary['status_msg'] = 'Test hash matches baseline hash.'
521+
else: # hash-diff
522+
summary['status'] = 'failed'
523+
summary['status_msg'] = (f"Hash {test_hash} doesn't match hash "
524+
f"{baseline_hash} in library "
525+
f"{hash_library_filename} for test {hash_name}.")
521526

522527
# Save the figure for later summary (will be removed later if not needed)
523528
test_image = (result_dir / "result.png").absolute()
524529
fig.savefig(str(test_image), **savefig_kwargs)
525530
summary['result_image'] = str(test_image.relative_to(self.results_dir))
526531

527-
if not new_test:
528-
if test_hash == hash_library[hash_name]:
529-
hash_comparison_pass = True
530-
summary['status'] = 'passed'
531-
summary['status_msg'] = 'Test hash matches baseline hash.'
532-
else:
533-
error_message = (f"Hash {test_hash} doesn't match hash "
534-
f"{hash_library[hash_name]} in library "
535-
f"{hash_library_filename} for test {hash_name}.")
536-
summary['status'] = 'failed'
537-
summary['status_msg'] = 'Test hash does not match baseline hash.'
538-
539-
# If the compare has only been specified with hash and not baseline
540-
# dir, don't attempt to find a baseline image at the default path.
541-
if not hash_comparison_pass and not self.baseline_directory_specified(item) or new_test:
542-
return error_message
532+
# Hybrid mode (hash and image comparison)
533+
if self.baseline_directory_specified(item):
534+
535+
# Skip image comparison if hash matches (unless `--mpl-results-always`)
536+
if hash_comparison_pass and not self.results_always:
537+
return
543538

544-
# If this is not a new test try and get the baseline image.
545-
if not new_test:
546-
baseline_error = None
547-
baseline_summary = {}
548-
# Ignore Errors here as it's possible the reference image dosen't exist yet.
549-
try:
550-
baseline_image_path = self.obtain_baseline_image(item, result_dir)
551-
baseline_image = baseline_image_path
552-
if baseline_image and not baseline_image.exists():
553-
baseline_image = None
554-
# Get the baseline and generate a diff image, always so that
555-
# --mpl-results-always can be respected.
539+
# Run image comparison
540+
baseline_summary = {} # summary for image comparison to merge with hash comparison summary
541+
try: # Ignore all errors as success does not influence the overall test result
556542
baseline_comparison = self.compare_image_to_baseline(item, fig, result_dir,
557543
summary=baseline_summary)
558-
except Exception as e:
559-
baseline_image = None
560-
baseline_error = e
561-
for k in ['baseline_image', 'diff_image', 'rms', 'tolerance', 'result_image']:
562-
summary[k] = summary[k] or baseline_summary.get(k)
563-
564-
# If the hash comparison passes then return
565-
if hash_comparison_pass:
544+
except Exception as baseline_error: # Append to test error later
545+
baseline_comparison = str(baseline_error)
546+
else: # Update main summary
547+
for k in ['baseline_image', 'diff_image', 'rms', 'tolerance', 'result_image']:
548+
summary[k] = summary[k] or baseline_summary.get(k)
549+
550+
# Append the log from image comparison
551+
r = baseline_comparison or "The comparison to the baseline image succeeded."
552+
summary['status_msg'] += ("\n\n"
553+
"Image comparison test\n"
554+
"---------------------\n") + r
555+
556+
if hash_comparison_pass: # Return None to indicate test passed
566557
return
567-
568-
if baseline_image is None:
569-
error_message += f"\nUnable to find baseline image for {item}."
570-
if baseline_error:
571-
error_message += f"\n{baseline_error}"
572-
summary['status'] = 'failed'
573-
summary['status_msg'] = error_message
574-
return error_message
575-
576-
# Override the tolerance (if not explicitly set) to 0 as the hashes are not forgiving
577-
tolerance = compare.kwargs.get('tolerance', None)
578-
if not tolerance:
579-
compare.kwargs['tolerance'] = 0
580-
581-
comparison_error = (baseline_comparison or
582-
"\nHowever, the comparison to the baseline image succeeded.")
583-
584-
error_message = f"{error_message}\n{comparison_error}"
585-
summary['status'] = 'failed'
586-
summary['status_msg'] = error_message
587-
return error_message
558+
return summary['status_msg']
588559

589560
def pytest_runtest_setup(self, item): # noqa
590561

0 commit comments

Comments
 (0)