@@ -128,7 +128,7 @@ def wrapper(*args, **kwargs):
128128 item .obj = figure_interceptor (plugin , item .obj )
129129
130130
131- def pytest_report_header (config , startdir ):
131+ def pytest_report_header ():
132132 import matplotlib
133133 import matplotlib .ft2font
134134 return ["Matplotlib: {0}" .format (matplotlib .__version__ ),
@@ -803,67 +803,76 @@ def pytest_runtest_call(self, item): # noqa
803803
804804 # Run test and get figure object
805805 wrap_figure_interceptor (self , item )
806- yield
807- if test_name not in self .return_value :
808- # Test function did not complete successfully
809- summary ['status' ] = 'failed'
810- summary ['status_msg' ] = ('Test function raised an exception '
811- 'before returning a figure.' )
812- self ._test_results [test_name ] = summary
813- return
814- fig = self .return_value [test_name ]
815-
816- if remove_text :
817- remove_ticks_and_titles (fig )
818-
819- result_dir = self .make_test_results_dir (item )
820-
821- # What we do now depends on whether we are generating the
822- # reference images or simply running the test.
823- if self .generate_dir is not None :
824- summary ['status' ] = 'skipped'
825- summary ['image_status' ] = 'generated'
826- summary ['status_msg' ] = 'Skipped test, since generating image.'
827- generate_image = self .generate_baseline_image (item , fig )
828- if self .results_always : # Make baseline image available in HTML
829- result_image = (result_dir / f"baseline.{ ext } " ).absolute ()
830- shutil .copy (generate_image , result_image )
831- summary ['baseline_image' ] = \
832- result_image .relative_to (self .results_dir ).as_posix ()
833-
834- if self .generate_hash_library is not None :
835- summary ['hash_status' ] = 'generated'
836- image_hash = self .generate_image_hash (item , fig )
837- self ._generated_hash_library [test_name ] = image_hash
838- summary ['baseline_hash' ] = image_hash
839-
840- # Only test figures if not generating images
841- if self .generate_dir is None :
842- # Compare to hash library
843- if self .hash_library or compare .kwargs .get ('hash_library' , None ):
844- msg = self .compare_image_to_hash_library (item , fig , result_dir , summary = summary )
845-
846- # Compare against a baseline if specified
847- else :
848- msg = self .compare_image_to_baseline (item , fig , result_dir , summary = summary )
849806
850- close_mpl_figure ( fig )
851-
852- if msg is None :
853- if not self .results_always :
854- shutil . rmtree ( result_dir )
855- for image_type in [ 'baseline_image' , 'diff_image' , 'result_image' ]:
856- summary [image_type ] = None # image no longer exists
857- else :
807+ # See https://github.com/pytest-dev/pytest/issues/11714
808+ result = yield
809+ try :
810+ if test_name not in self .return_value :
811+ # Test function did not complete successfully
812+ summary [ 'status' ] = 'failed'
813+ summary ['status_msg' ] = ( 'Test function raised an exception '
814+ 'before returning a figure.' )
858815 self ._test_results [test_name ] = summary
859- pytest .fail (msg , pytrace = False )
816+ return
817+ fig = self .return_value [test_name ]
818+
819+ if remove_text :
820+ remove_ticks_and_titles (fig )
821+
822+ result_dir = self .make_test_results_dir (item )
823+
824+ # What we do now depends on whether we are generating the
825+ # reference images or simply running the test.
826+ if self .generate_dir is not None :
827+ summary ['status' ] = 'skipped'
828+ summary ['image_status' ] = 'generated'
829+ summary ['status_msg' ] = 'Skipped test, since generating image.'
830+ generate_image = self .generate_baseline_image (item , fig )
831+ if self .results_always : # Make baseline image available in HTML
832+ result_image = (result_dir / f"baseline.{ ext } " ).absolute ()
833+ shutil .copy (generate_image , result_image )
834+ summary ['baseline_image' ] = \
835+ result_image .relative_to (self .results_dir ).as_posix ()
836+
837+ if self .generate_hash_library is not None :
838+ summary ['hash_status' ] = 'generated'
839+ image_hash = self .generate_image_hash (item , fig )
840+ self ._generated_hash_library [test_name ] = image_hash
841+ summary ['baseline_hash' ] = image_hash
842+
843+ # Only test figures if not generating images
844+ if self .generate_dir is None :
845+ # Compare to hash library
846+ if self .hash_library or compare .kwargs .get ('hash_library' , None ):
847+ msg = self .compare_image_to_hash_library (item , fig , result_dir , summary = summary )
848+
849+ # Compare against a baseline if specified
850+ else :
851+ msg = self .compare_image_to_baseline (item , fig , result_dir , summary = summary )
852+
853+ close_mpl_figure (fig )
854+
855+ if msg is None :
856+ if not self .results_always :
857+ shutil .rmtree (result_dir )
858+ for image_type in ['baseline_image' , 'diff_image' , 'result_image' ]:
859+ summary [image_type ] = None # image no longer exists
860+ else :
861+ self ._test_results [test_name ] = summary
862+ pytest .fail (msg , pytrace = False )
860863
861- close_mpl_figure (fig )
864+ close_mpl_figure (fig )
862865
863- self ._test_results [test_name ] = summary
866+ self ._test_results [test_name ] = summary
864867
865- if summary ['status' ] == 'skipped' :
866- pytest .skip (summary ['status_msg' ])
868+ if summary ['status' ] == 'skipped' :
869+ pytest .skip (summary ['status_msg' ])
870+ except BaseException as e :
871+ if hasattr (result , "force_exception" ): # pluggy>=1.2.0
872+ result .force_exception (e )
873+ else :
874+ result ._result = None
875+ result ._excinfo = (type (e ), e , e .__traceback__ )
867876
868877 def generate_summary_json (self ):
869878 json_file = self .results_dir / 'results.json'
0 commit comments