@@ -120,11 +120,10 @@ def test_fail():
120
120
"""
121
121
122
122
123
- def test_fails (tmpdir ):
124
-
125
- test_file = tmpdir .join ('test.py' ).strpath
126
- with open (test_file , 'w' ) as f :
127
- f .write (TEST_FAILING )
123
+ def test_fails (tmp_path ):
124
+ test_file = tmp_path / "test.py"
125
+ test_file .write_text (TEST_FAILING )
126
+ test_file = str (test_file )
128
127
129
128
# If we use --mpl, it should detect that the figure is wrong
130
129
code = call_pytest (['--mpl' , test_file ])
@@ -147,16 +146,15 @@ def test_output_dir():
147
146
"""
148
147
149
148
150
- def test_output_dir (tmpdir ):
151
- test_file = tmpdir .join ('test.py' ).strpath
152
- with open (test_file , 'w' ) as f :
153
- f .write (TEST_OUTPUT_DIR )
149
+ def test_output_dir (tmp_path ):
150
+ test_file = tmp_path / "test.py"
151
+ test_file .write_text (TEST_OUTPUT_DIR )
154
152
155
- output_dir = tmpdir . join ( ' test_output_dir' )
153
+ output_dir = tmp_path / " test_output_dir"
156
154
157
155
# When we run the test, we should get output images where we specify
158
156
code = call_pytest ([f'--mpl-results-path={ output_dir } ' ,
159
- '--mpl' , test_file ])
157
+ '--mpl' , str ( test_file ) ])
160
158
161
159
assert code != 0
162
160
assert output_dir .exists ()
@@ -175,13 +173,14 @@ def test_gen():
175
173
"""
176
174
177
175
178
- def test_generate (tmpdir ):
176
+ def test_generate (tmp_path ):
179
177
180
- test_file = tmpdir . join ( ' test.py' ). strpath
181
- with open ( test_file , 'w' ) as f :
182
- f . write ( TEST_GENERATE )
178
+ test_file = tmp_path / " test.py"
179
+ test_file . write_text ( TEST_GENERATE )
180
+ test_file = str ( test_file )
183
181
184
- gen_dir = tmpdir .mkdir ('spam' ).mkdir ('egg' ).strpath
182
+ gen_dir = tmp_path / "spam" / "egg"
183
+ gen_dir .mkdir (parents = True )
185
184
186
185
# If we don't generate, the test will fail
187
186
assert_pytest_fails_with (['--mpl' , test_file ], 'Image file not found for comparison test' )
@@ -301,11 +300,10 @@ def test_hash_fails():
301
300
"""
302
301
303
302
304
- def test_hash_fails (tmpdir ):
305
-
306
- test_file = tmpdir .join ('test.py' ).strpath
307
- with open (test_file , 'w' , encoding = 'ascii' ) as f :
308
- f .write (TEST_FAILING_HASH )
303
+ def test_hash_fails (tmp_path ):
304
+ test_file = tmp_path / "test.py"
305
+ test_file .write_text (TEST_FAILING_HASH , encoding = "ascii" )
306
+ test_file = str (test_file )
309
307
310
308
# If we use --mpl, it should detect that the figure is wrong
311
309
output = assert_pytest_fails_with (['--mpl' , test_file ], "doesn't match hash FAIL in library" )
@@ -340,11 +338,11 @@ def test_hash_fail_hybrid():
340
338
341
339
342
340
@pytest .mark .skipif (ftv != '261' , reason = "Incorrect freetype version for hash check" )
343
- def test_hash_fail_hybrid (tmpdir ):
341
+ def test_hash_fail_hybrid (tmp_path ):
344
342
345
- test_file = tmpdir . join ( ' test.py' ). strpath
346
- with open ( test_file , 'w' , encoding = ' ascii' ) as f :
347
- f . write ( TEST_FAILING_HYBRID )
343
+ test_file = tmp_path / " test.py"
344
+ test_file . write_text ( TEST_FAILING_HYBRID , encoding = " ascii" )
345
+ test_file = str ( test_file )
348
346
349
347
# Assert that image comparison runs and fails
350
348
output = assert_pytest_fails_with (['--mpl' , test_file ,
@@ -382,18 +380,18 @@ def test_hash_fails():
382
380
383
381
384
382
@pytest .mark .skipif (ftv != '261' , reason = "Incorrect freetype version for hash check" )
385
- def test_hash_fail_new_hashes (tmpdir ):
383
+ def test_hash_fail_new_hashes (tmp_path ):
386
384
# Check that the hash comparison fails even if a new hash file is requested
387
- test_file = tmpdir . join ( ' test.py' ). strpath
388
- with open ( test_file , 'w' , encoding = ' ascii' ) as f :
389
- f . write ( TEST_FAILING_NEW_HASH )
385
+ test_file = tmp_path / " test.py"
386
+ test_file . write_text ( TEST_FAILING_NEW_HASH , encoding = " ascii" )
387
+ test_file = str ( test_file )
390
388
391
389
# Assert that image comparison runs and fails
392
390
assert_pytest_fails_with (['--mpl' , test_file ,
393
391
f'--mpl-hash-library={ fail_hash_library } ' ],
394
392
"doesn't match hash FAIL in library" )
395
393
396
- hash_file = tmpdir . join ( ' new_hashes.json' ). strpath
394
+ hash_file = tmp_path / " new_hashes.json"
397
395
# Assert that image comparison runs and fails
398
396
assert_pytest_fails_with (['--mpl' , test_file ,
399
397
f'--mpl-hash-library={ fail_hash_library } ' ,
@@ -413,11 +411,10 @@ def test_hash_missing():
413
411
"""
414
412
415
413
416
- def test_hash_missing (tmpdir ):
417
-
418
- test_file = tmpdir .join ('test.py' ).strpath
419
- with open (test_file , 'w' ) as f :
420
- f .write (TEST_MISSING_HASH )
414
+ def test_hash_missing (tmp_path ):
415
+ test_file = tmp_path / "test.py"
416
+ test_file .write_text (TEST_MISSING_HASH )
417
+ test_file = str (test_file )
421
418
422
419
# Assert fails if hash library missing
423
420
assert_pytest_fails_with (['--mpl' , test_file , '--mpl-hash-library=/not/a/path' ],
@@ -450,31 +447,26 @@ def test_unmodified(): return plot()
450
447
451
448
452
449
@pytest .mark .skipif (not hash_library .exists (), reason = "No hash library for this mpl version" )
453
- def test_results_always (tmpdir ):
450
+ def test_results_always (tmp_path ):
451
+ test_file = tmp_path / "test.py"
452
+ test_file .write_text (TEST_RESULTS_ALWAYS )
453
+ results_path = tmp_path / "results"
454
+ results_path .mkdir ()
454
455
455
- test_file = tmpdir .join ('test.py' ).strpath
456
- with open (test_file , 'w' ) as f :
457
- f .write (TEST_RESULTS_ALWAYS )
458
- results_path = tmpdir .mkdir ('results' )
459
-
460
- code = call_pytest (['--mpl' , test_file , '--mpl-results-always' ,
456
+ code = call_pytest (['--mpl' , str (test_file ), '--mpl-results-always' ,
461
457
rf'--mpl-hash-library={ hash_library } ' ,
462
458
rf'--mpl-baseline-path={ baseline_dir_abs } ' ,
463
459
'--mpl-generate-summary=html,json,basic-html' ,
464
- rf'--mpl-results-path={ results_path . strpath } ' ])
460
+ rf'--mpl-results-path={ results_path } ' ])
465
461
assert code == 0 # hashes correct, so all should pass
466
462
467
463
# assert files for interactive HTML exist
468
- assert results_path .join ('fig_comparison.html' ).exists ()
469
- assert results_path .join ('styles.css' ).exists ()
470
- assert results_path .join ('extra.js' ).exists ()
471
-
472
- comparison_file = results_path .join ('fig_comparison_basic.html' )
473
- with open (comparison_file , 'r' ) as f :
474
- html = f .read ()
464
+ assert (results_path / "fig_comparison.html" ).exists ()
465
+ assert (results_path / "styles.css" ).exists ()
466
+ assert (results_path / "extra.js" ).exists ()
475
467
476
- json_file = results_path . join ( 'results.json' )
477
- with open ( json_file , 'r' ) as f :
468
+ html = ( results_path / "fig_comparison_basic.html" ). read_text ( )
469
+ with ( results_path / "results.json" ). open ( "r" ) as f :
478
470
json_results = json .load (f )
479
471
480
472
# each test, and which images should exist
@@ -495,7 +487,7 @@ def test_results_always(tmpdir):
495
487
496
488
for image_type in ['baseline' , 'result-failed-diff' , 'result' ]:
497
489
image = f'{ test_name } /{ image_type } .png'
498
- image_exists = results_path . join ( * image . split ( '/' ) ).exists ()
490
+ image_exists = ( results_path / image ).exists ()
499
491
json_image_key = f"{ image_type .split ('-' )[- 1 ]} _image"
500
492
if image_type in exists : # assert image so pytest prints it on error
501
493
assert image and image_exists
@@ -554,11 +546,11 @@ def test_fails(self):
554
546
TEST_FAILING_CLASS_SETUP_METHOD ,
555
547
TEST_FAILING_UNITTEST_TESTCASE ,
556
548
])
557
- def test_class_fail (code , tmpdir ):
549
+ def test_class_fail (code , tmp_path ):
558
550
559
- test_file = tmpdir . join ( ' test.py' ). strpath
560
- with open ( test_file , 'w' ) as f :
561
- f . write ( code )
551
+ test_file = tmp_path / " test.py"
552
+ test_file . write_text ( code )
553
+ test_file = str ( test_file )
562
554
563
555
# Assert fails if hash library missing
564
556
assert_pytest_fails_with (['--mpl' , test_file , '--mpl-hash-library=/not/a/path' ],
0 commit comments