diff --git a/lib/iris/tests/__init__.py b/lib/iris/tests/__init__.py index 7b88065d38..c1df4f628b 100644 --- a/lib/iris/tests/__init__.py +++ b/lib/iris/tests/__init__.py @@ -39,6 +39,7 @@ import subprocess import sys import threading +from typing import Dict, List import unittest from unittest import mock import warnings @@ -878,7 +879,9 @@ def check_graphic(self): unique_id = self._unique_id() repo_fname = os.path.join(_RESULT_PATH, "imagerepo.json") with open(repo_fname, "rb") as fi: - repo = json.load(codecs.getreader("utf-8")(fi)) + repo: Dict[str, List[str]] = json.load( + codecs.getreader("utf-8")(fi) + ) try: #: The path where the images generated by the tests should go. @@ -949,13 +952,16 @@ def _create_missing(): phash = imagehash.phash(Image.open(buffer), hash_size=_HASH_SIZE) if unique_id not in repo: - if dev_mode: - _create_missing() - else: - figure.savefig(result_fname) - emsg = "Missing image test result: {}." - raise AssertionError(emsg.format(unique_id)) - else: + # The unique id might not be fully qualified, e.g. + # expects iris.tests.test_quickplot.TestLabels.test_contour.0, + # but got test_quickplot.TestLabels.test_contour.0 + # if we find single partial match from end of the key + # then use that, else fall back to the unknown id state. + matches = [key for key in repo if key.endswith(unique_id)] + if len(matches) == 1: + unique_id = matches[0] + + if unique_id in repo: uris = repo[unique_id] # Extract the hex basename strings from the uris. hexes = [ @@ -984,6 +990,13 @@ def _create_missing(): else: emsg = "Image comparison failed: {}" raise AssertionError(emsg.format(msg)) + else: + if dev_mode: + _create_missing() + else: + figure.savefig(result_fname) + emsg = "Missing image test result: {}." + raise AssertionError(emsg.format(unique_id)) if _DISPLAY_FIGURES: plt.show()