30
30
"hashes" / hash_filename )
31
31
32
32
fail_hash_library = Path (__file__ ).parent / "baseline" / "test_hash_lib.json"
33
+ baseline_dir_abs = Path (__file__ ).parent / "baseline" / baseline_subdir
34
+ hash_baseline_dir_abs = Path (__file__ ).parent / "baseline" / "hybrid"
33
35
34
36
35
37
WIN = sys .platform .startswith ('win' )
@@ -42,6 +44,15 @@ def call_pytest(args):
42
44
return subprocess .call ([sys .executable , '-m' , 'pytest' , '-s' ] + args )
43
45
44
46
47
+ def assert_pytest_fails_with (args , output_substring ):
48
+ try :
49
+ subprocess .check_output ([sys .executable , '-m' , 'pytest' , '-s' ] + args )
50
+ except subprocess .CalledProcessError as exc :
51
+ output = exc .output .decode ()
52
+ assert output_substring in output , output
53
+ return output
54
+
55
+
45
56
@pytest .mark .mpl_image_compare (baseline_dir = baseline_dir_local ,
46
57
tolerance = DEFAULT_TOLERANCE )
47
58
def test_succeeds ():
@@ -172,10 +183,7 @@ def test_generate(tmpdir):
172
183
gen_dir = tmpdir .mkdir ('spam' ).mkdir ('egg' ).strpath
173
184
174
185
# If we don't generate, the test will fail
175
- try :
176
- subprocess .check_output ([sys .executable , '-m' , 'pytest' , '-s' , '--mpl' , test_file ])
177
- except subprocess .CalledProcessError as exc :
178
- assert b'Image file not found for comparison test' in exc .output , exc .output .decode ()
186
+ assert_pytest_fails_with (['--mpl' , test_file ], 'Image file not found for comparison test' )
179
187
180
188
# If we do generate, the test should succeed and a new file will appear
181
189
code = call_pytest ([f'--mpl-generate-path={ gen_dir } ' , test_file ])
@@ -282,10 +290,50 @@ def test_hash_fails(tmpdir):
282
290
f .write (TEST_FAILING_HASH )
283
291
284
292
# If we use --mpl, it should detect that the figure is wrong
285
- try :
286
- subprocess .check_output ([sys .executable , '-m' , 'pytest' , '-s' , '--mpl' , test_file ])
287
- except subprocess .CalledProcessError as exc :
288
- assert b"doesn't match hash FAIL in library" in exc .output , exc .output .decode ()
293
+ output = assert_pytest_fails_with (['--mpl' , test_file ], "doesn't match hash FAIL in library" )
294
+ # We didn't specify a baseline dir so we shouldn't attempt to find one
295
+ assert "Unable to find baseline image" not in output , output
296
+
297
+ # If we don't use --mpl option, the test should succeed
298
+ code = call_pytest ([test_file ])
299
+ assert code == 0
300
+
301
+
302
+ TEST_FAILING_HYBRID = f"""
303
+ import pytest
304
+ import matplotlib.pyplot as plt
305
+ @pytest.mark.mpl_image_compare(hash_library="{ fail_hash_library } ")
306
+ def test_hash_fail_hybrid():
307
+ fig = plt.figure()
308
+ ax = fig.add_subplot(1,1,1)
309
+ ax.plot([1,2,3])
310
+ return fig
311
+ """
312
+
313
+
314
+ def test_hash_fail_hybrid (tmpdir ):
315
+
316
+ test_file = tmpdir .join ('test.py' ).strpath
317
+ with open (test_file , 'w' , encoding = 'ascii' ) as f :
318
+ f .write (TEST_FAILING_HYBRID )
319
+
320
+ # Assert that image comparison runs and fails
321
+ output = assert_pytest_fails_with (['--mpl' , test_file ,
322
+ f'--mpl-baseline-path={ hash_baseline_dir_abs / "fail" } ' ],
323
+ "doesn't match hash FAIL in library" )
324
+ assert "Error: Image files did not match." in output , output
325
+
326
+ # Assert reports missing baseline image
327
+ output = assert_pytest_fails_with (['--mpl' , test_file ,
328
+ '--mpl-baseline-path=/not/a/path' ],
329
+ "doesn't match hash FAIL in library" )
330
+ assert "Unable to find baseline image" in output , output
331
+
332
+ # Assert reports image comparison succeeds
333
+ output = assert_pytest_fails_with (['--mpl' , test_file ,
334
+ f'--mpl-baseline-path={ hash_baseline_dir_abs / "succeed" } ' ],
335
+ "doesn't match hash FAIL in library" )
336
+ assert "However, the comparison to the baseline image succeeded." in output , output
289
337
290
338
# If we don't use --mpl option, the test should succeed
291
339
code = call_pytest ([test_file ])
@@ -310,12 +358,13 @@ def test_hash_missing(tmpdir):
310
358
with open (test_file , 'w' ) as f :
311
359
f .write (TEST_MISSING_HASH )
312
360
313
- # If we use --mpl, it should detect that the figure is wrong
314
- try :
315
- subprocess .check_output ([sys .executable , '-m' , 'pytest' , '-s' , '--mpl' , test_file ,
316
- f'--mpl-hash-library={ os .path .join (baseline_dir , "test_hash_lib.json" )} ' ]) # noqa
317
- except subprocess .CalledProcessError as exc :
318
- assert b"Can't find hash library at path" in exc .output , exc .output .decode ()
361
+ # Assert fails if hash library missing
362
+ assert_pytest_fails_with (['--mpl' , test_file , '--mpl-hash-library=/not/a/path' ],
363
+ "Can't find hash library at path" )
364
+
365
+ # Assert fails if hash not in library
366
+ assert_pytest_fails_with (['--mpl' , test_file , f'--mpl-hash-library={ fail_hash_library } ' ],
367
+ "Hash for test 'test.test_hash_missing' not found in" )
319
368
320
369
# If we don't use --mpl option, the test should succeed
321
370
code = call_pytest ([test_file ])
0 commit comments