@@ -472,16 +472,15 @@ def _download_file(self, baseline, filename):
472
472
self .logger .info (f'Downloading { base_url + filename } failed: { repr (e )} ' )
473
473
else :
474
474
break
475
- else :
476
- raise Exception ("Could not download baseline image from any of the "
477
- "available URLs" )
475
+ else : # Could not download baseline image from any of the available URLs
476
+ return
478
477
result_dir = Path (tempfile .mkdtemp ())
479
478
filename = result_dir / 'downloaded'
480
479
with open (str (filename ), 'wb' ) as tmpfile :
481
480
tmpfile .write (content )
482
481
return Path (filename )
483
482
484
- def obtain_baseline_image (self , item , target_dir ):
483
+ def obtain_baseline_image (self , item ):
485
484
"""
486
485
Copy the baseline image to our working directory.
487
486
@@ -545,8 +544,6 @@ def compare_image_to_baseline(self, item, fig, result_dir, summary=None):
545
544
546
545
ext = self ._file_extension (item )
547
546
548
- baseline_image_ref = self .obtain_baseline_image (item , result_dir )
549
-
550
547
test_image = (result_dir / f"result.{ ext } " ).absolute ()
551
548
self .save_figure (item , fig , test_image )
552
549
@@ -555,11 +552,20 @@ def compare_image_to_baseline(self, item, fig, result_dir, summary=None):
555
552
else :
556
553
summary ['result_image' ] = (result_dir / f"result_{ ext } .png" ).relative_to (self .results_dir ).as_posix ()
557
554
558
- if not os .path .exists (baseline_image_ref ):
555
+ baseline_image_ref = self .obtain_baseline_image (item )
556
+
557
+ baseline_missing = None
558
+ if baseline_image_ref is None :
559
+ baseline_missing = ("Could not download the baseline image from "
560
+ "any of the available URLs.\n " )
561
+ elif not os .path .exists (baseline_image_ref ):
562
+ baseline_missing = ("Image file not found for comparison test in: \n \t "
563
+ f"{ self .get_baseline_directory (item )} \n " )
564
+
565
+ if baseline_missing :
559
566
summary ['status' ] = 'failed'
560
567
summary ['image_status' ] = 'missing'
561
- error_message = ("Image file not found for comparison test in: \n \t "
562
- f"{ self .get_baseline_directory (item )} \n "
568
+ error_message = (baseline_missing +
563
569
"(This is expected for new tests.)\n "
564
570
"Generated Image: \n \t "
565
571
f"{ test_image } " )
@@ -728,6 +734,7 @@ def compare_image_to_hash_library(self, item, fig, result_dir, summary=None):
728
734
baseline_comparison = self .compare_image_to_baseline (item , fig , result_dir ,
729
735
summary = baseline_summary )
730
736
except Exception as baseline_error : # Append to test error later
737
+ summary ['image_status' ] = 'diff' # (not necessarily diff, but makes user aware)
731
738
baseline_comparison = str (baseline_error )
732
739
else : # Update main summary
733
740
for k in ['image_status' , 'baseline_image' , 'diff_image' ,
@@ -768,25 +775,14 @@ def pytest_runtest_call(self, item): # noqa
768
775
769
776
with plt .style .context (style , after_reset = True ), switch_backend (backend ):
770
777
771
- # Run test and get figure object
772
- wrap_figure_interceptor (self , item )
773
- yield
774
778
test_name = generate_test_name (item )
775
- if test_name not in self .return_value :
776
- # Test function did not complete successfully
777
- return
778
- fig = self .return_value [test_name ]
779
-
780
- if remove_text :
781
- remove_ticks_and_titles (fig )
782
-
783
- result_dir = self .make_test_results_dir (item )
784
779
780
+ # Store fallback summary in case of exceptions
785
781
summary = {
786
- 'status' : None ,
782
+ 'status' : 'failed' ,
787
783
'image_status' : None ,
788
784
'hash_status' : None ,
789
- 'status_msg' : None ,
785
+ 'status_msg' : 'An exception was raised while testing the figure.' ,
790
786
'baseline_image' : None ,
791
787
'diff_image' : None ,
792
788
'rms' : None ,
@@ -795,6 +791,24 @@ def pytest_runtest_call(self, item): # noqa
795
791
'baseline_hash' : None ,
796
792
'result_hash' : None ,
797
793
}
794
+ self ._test_results [test_name ] = summary
795
+
796
+ # Run test and get figure object
797
+ wrap_figure_interceptor (self , item )
798
+ yield
799
+ if test_name not in self .return_value :
800
+ # Test function did not complete successfully
801
+ summary ['status' ] = 'failed'
802
+ summary ['status_msg' ] = ('Test function raised an exception '
803
+ 'before returning a figure.' )
804
+ self ._test_results [test_name ] = summary
805
+ return
806
+ fig = self .return_value [test_name ]
807
+
808
+ if remove_text :
809
+ remove_ticks_and_titles (fig )
810
+
811
+ result_dir = self .make_test_results_dir (item )
798
812
799
813
# What we do now depends on whether we are generating the
800
814
# reference images or simply running the test.
0 commit comments