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