3434
3535
3636def run_subtest (baseline_summary_name , tmp_path , args , summaries = None , xfail = True ,
37+ has_result_hashes = False ,
3738 update_baseline = UPDATE_BASELINE , update_summary = UPDATE_SUMMARY ):
3839 """ Run pytest (within pytest) and check JSON summary report.
3940
@@ -49,6 +50,9 @@ def run_subtest(baseline_summary_name, tmp_path, args, summaries=None, xfail=Tru
4950 Summaries to generate in addition to `json`.
5051 xfail : bool, optional, default=True
5152 Whether the overall pytest run should fail.
53+ has_result_hashes : bool or str, optional, default=False
54+ Whether a hash library is expected to exist in the results directory.
55+ If a string, this is the name of the expected results file.
5256 """
5357 # Parse arguments
5458 if summaries is None :
@@ -110,6 +114,24 @@ def run_subtest(baseline_summary_name, tmp_path, args, summaries=None, xfail=Tru
110114 # Ensure reported images exist
111115 assert_existence (result_summary , path = results_path )
112116
117+ # Get expected name for the hash library saved to the results directory
118+ if isinstance (has_result_hashes , str ):
119+ result_hash_file = tmp_path / 'results' / has_result_hashes
120+ has_result_hashes = True # convert to bool after processing str
121+ else :
122+ result_hash_file = tmp_path / 'results' / HASH_LIBRARY .name
123+
124+ # Compare the generated hash library to the expected hash library
125+ if has_result_hashes :
126+ assert result_hash_file .exists ()
127+ with open (RESULT_LIBRARY , "r" ) as f :
128+ baseline = json .load (f )
129+ with open (result_hash_file , "r" ) as f :
130+ result = json .load (f )
131+ diff_summary ({'a' : baseline }, {'a' : result })
132+ else :
133+ assert not result_hash_file .exists ()
134+
113135
114136def test_default (tmp_path ):
115137 run_subtest ('test_default' , tmp_path , [])
@@ -128,21 +150,24 @@ def test_hybrid(tmp_path):
128150@pytest .mark .skipif (not HASH_LIBRARY .exists (), reason = "No hash library for this mpl version" )
129151def test_results_always (tmp_path ):
130152 run_subtest ('test_results_always' , tmp_path ,
131- [HASH_LIBRARY_FLAG , BASELINE_IMAGES_FLAG_ABS , '--mpl-results-always' ])
153+ [HASH_LIBRARY_FLAG , BASELINE_IMAGES_FLAG_ABS , '--mpl-results-always' ],
154+ has_result_hashes = True )
132155
133156
134157@pytest .mark .skipif (not HASH_LIBRARY .exists (), reason = "No hash library for this mpl version" )
135158def test_html (tmp_path ):
136159 run_subtest ('test_results_always' , tmp_path ,
137- [HASH_LIBRARY_FLAG , BASELINE_IMAGES_FLAG_ABS ], summaries = ['html' ])
160+ [HASH_LIBRARY_FLAG , BASELINE_IMAGES_FLAG_ABS ], summaries = ['html' ],
161+ has_result_hashes = True )
138162 assert (tmp_path / 'results' / 'fig_comparison.html' ).exists ()
139163 assert (tmp_path / 'results' / 'extra.js' ).exists ()
140164 assert (tmp_path / 'results' / 'styles.css' ).exists ()
141165
142166
143167@pytest .mark .skipif (not HASH_LIBRARY .exists (), reason = "No hash library for this mpl version" )
144168def test_html_hashes_only (tmp_path ):
145- run_subtest ('test_html_hashes_only' , tmp_path , [HASH_LIBRARY_FLAG ], summaries = ['html' ])
169+ run_subtest ('test_html_hashes_only' , tmp_path , [HASH_LIBRARY_FLAG ], summaries = ['html' ],
170+ has_result_hashes = True )
146171 assert (tmp_path / 'results' / 'fig_comparison.html' ).exists ()
147172 assert (tmp_path / 'results' / 'extra.js' ).exists ()
148173 assert (tmp_path / 'results' / 'styles.css' ).exists ()
@@ -158,5 +183,6 @@ def test_html_images_only(tmp_path):
158183@pytest .mark .skipif (not HASH_LIBRARY .exists (), reason = "No hash library for this mpl version" )
159184def test_basic_html (tmp_path ):
160185 run_subtest ('test_results_always' , tmp_path ,
161- [HASH_LIBRARY_FLAG , * BASELINE_IMAGES_FLAG_REL ], summaries = ['basic-html' ])
186+ [HASH_LIBRARY_FLAG , * BASELINE_IMAGES_FLAG_REL ], summaries = ['basic-html' ],
187+ has_result_hashes = True )
162188 assert (tmp_path / 'results' / 'fig_comparison_basic.html' ).exists ()
0 commit comments