@@ -120,11 +120,10 @@ def test_fail():
120120"""
121121
122122
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 )
128127
129128 # If we use --mpl, it should detect that the figure is wrong
130129 code = call_pytest (['--mpl' , test_file ])
@@ -147,16 +146,15 @@ def test_output_dir():
147146"""
148147
149148
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 )
154152
155- output_dir = tmpdir . join ( ' test_output_dir' )
153+ output_dir = tmp_path / " test_output_dir"
156154
157155 # When we run the test, we should get output images where we specify
158156 code = call_pytest ([f'--mpl-results-path={ output_dir } ' ,
159- '--mpl' , test_file ])
157+ '--mpl' , str ( test_file ) ])
160158
161159 assert code != 0
162160 assert output_dir .exists ()
@@ -175,13 +173,14 @@ def test_gen():
175173"""
176174
177175
178- def test_generate (tmpdir ):
176+ def test_generate (tmp_path ):
179177
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 )
183181
184- gen_dir = tmpdir .mkdir ('spam' ).mkdir ('egg' ).strpath
182+ gen_dir = tmp_path / "spam" / "egg"
183+ gen_dir .mkdir (parents = True )
185184
186185 # If we don't generate, the test will fail
187186 assert_pytest_fails_with (['--mpl' , test_file ], 'Image file not found for comparison test' )
@@ -301,11 +300,10 @@ def test_hash_fails():
301300"""
302301
303302
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 )
309307
310308 # If we use --mpl, it should detect that the figure is wrong
311309 output = assert_pytest_fails_with (['--mpl' , test_file ], "doesn't match hash FAIL in library" )
@@ -340,11 +338,11 @@ def test_hash_fail_hybrid():
340338
341339
342340@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 ):
344342
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 )
348346
349347 # Assert that image comparison runs and fails
350348 output = assert_pytest_fails_with (['--mpl' , test_file ,
@@ -382,18 +380,18 @@ def test_hash_fails():
382380
383381
384382@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 ):
386384 # 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 )
390388
391389 # Assert that image comparison runs and fails
392390 assert_pytest_fails_with (['--mpl' , test_file ,
393391 f'--mpl-hash-library={ fail_hash_library } ' ],
394392 "doesn't match hash FAIL in library" )
395393
396- hash_file = tmpdir . join ( ' new_hashes.json' ). strpath
394+ hash_file = tmp_path / " new_hashes.json"
397395 # Assert that image comparison runs and fails
398396 assert_pytest_fails_with (['--mpl' , test_file ,
399397 f'--mpl-hash-library={ fail_hash_library } ' ,
@@ -413,11 +411,10 @@ def test_hash_missing():
413411"""
414412
415413
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 )
421418
422419 # Assert fails if hash library missing
423420 assert_pytest_fails_with (['--mpl' , test_file , '--mpl-hash-library=/not/a/path' ],
@@ -450,31 +447,26 @@ def test_unmodified(): return plot()
450447
451448
452449@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 ()
454455
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' ,
461457 rf'--mpl-hash-library={ hash_library } ' ,
462458 rf'--mpl-baseline-path={ baseline_dir_abs } ' ,
463459 '--mpl-generate-summary=html,json,basic-html' ,
464- rf'--mpl-results-path={ results_path . strpath } ' ])
460+ rf'--mpl-results-path={ results_path } ' ])
465461 assert code == 0 # hashes correct, so all should pass
466462
467463 # 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 ()
475467
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 :
478470 json_results = json .load (f )
479471
480472 # each test, and which images should exist
@@ -495,7 +487,7 @@ def test_results_always(tmpdir):
495487
496488 for image_type in ['baseline' , 'result-failed-diff' , 'result' ]:
497489 image = f'{ test_name } /{ image_type } .png'
498- image_exists = results_path . join ( * image . split ( '/' ) ).exists ()
490+ image_exists = ( results_path / image ).exists ()
499491 json_image_key = f"{ image_type .split ('-' )[- 1 ]} _image"
500492 if image_type in exists : # assert image so pytest prints it on error
501493 assert image and image_exists
@@ -554,11 +546,11 @@ def test_fails(self):
554546 TEST_FAILING_CLASS_SETUP_METHOD ,
555547 TEST_FAILING_UNITTEST_TESTCASE ,
556548])
557- def test_class_fail (code , tmpdir ):
549+ def test_class_fail (code , tmp_path ):
558550
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 )
562554
563555 # Assert fails if hash library missing
564556 assert_pytest_fails_with (['--mpl' , test_file , '--mpl-hash-library=/not/a/path' ],
0 commit comments