From e57d11efeaf67d932f48e3ba4c5453c713a259a1 Mon Sep 17 00:00:00 2001 From: "P. L. Lim" <2090236+pllim@users.noreply.github.com> Date: Tue, 9 Jan 2024 11:48:23 -0500 Subject: [PATCH] Remove args from pytest_report_header and fix up the other pytest 8 compat problem with old-style hook wrapper. Update flake8 setting to reflect what is actually in the code. --- pytest_mpl/plugin.py | 139 ++++++++++++++++++++++--------------------- setup.cfg | 6 +- 2 files changed, 73 insertions(+), 72 deletions(-) diff --git a/pytest_mpl/plugin.py b/pytest_mpl/plugin.py index 6648167..7b59184 100644 --- a/pytest_mpl/plugin.py +++ b/pytest_mpl/plugin.py @@ -132,19 +132,11 @@ def wrapper(*args, **kwargs): item.obj = figure_interceptor(plugin, item.obj) -if PYTEST_GE_8_0: - def pytest_report_header(config, start_path): - import matplotlib - import matplotlib.ft2font - return ["Matplotlib: {0}".format(matplotlib.__version__), - "Freetype: {0}".format(matplotlib.ft2font.__freetype_version__)] - -else: - def pytest_report_header(config, startdir): - import matplotlib - import matplotlib.ft2font - return ["Matplotlib: {0}".format(matplotlib.__version__), - "Freetype: {0}".format(matplotlib.ft2font.__freetype_version__)] +def pytest_report_header(): + import matplotlib + import matplotlib.ft2font + return ["Matplotlib: {0}".format(matplotlib.__version__), + "Freetype: {0}".format(matplotlib.ft2font.__freetype_version__)] def pytest_addoption(parser): @@ -815,67 +807,76 @@ def pytest_runtest_call(self, item): # noqa # Run test and get figure object wrap_figure_interceptor(self, item) - yield - if test_name not in self.return_value: - # Test function did not complete successfully - summary['status'] = 'failed' - summary['status_msg'] = ('Test function raised an exception ' - 'before returning a figure.') - self._test_results[test_name] = summary - return - fig = self.return_value[test_name] - - if remove_text: - remove_ticks_and_titles(fig) - - result_dir = self.make_test_results_dir(item) - - # What we do now depends on whether we are generating the - # reference images or simply running the test. - if self.generate_dir is not None: - summary['status'] = 'skipped' - summary['image_status'] = 'generated' - summary['status_msg'] = 'Skipped test, since generating image.' - generate_image = self.generate_baseline_image(item, fig) - if self.results_always: # Make baseline image available in HTML - result_image = (result_dir / f"baseline.{ext}").absolute() - shutil.copy(generate_image, result_image) - summary['baseline_image'] = \ - result_image.relative_to(self.results_dir).as_posix() - - if self.generate_hash_library is not None: - summary['hash_status'] = 'generated' - image_hash = self.generate_image_hash(item, fig) - self._generated_hash_library[test_name] = image_hash - summary['baseline_hash'] = image_hash - - # Only test figures if not generating images - if self.generate_dir is None: - # Compare to hash library - if self.hash_library or compare.kwargs.get('hash_library', None): - msg = self.compare_image_to_hash_library(item, fig, result_dir, summary=summary) - - # Compare against a baseline if specified - else: - msg = self.compare_image_to_baseline(item, fig, result_dir, summary=summary) - - close_mpl_figure(fig) - if msg is None: - if not self.results_always: - shutil.rmtree(result_dir) - for image_type in ['baseline_image', 'diff_image', 'result_image']: - summary[image_type] = None # image no longer exists - else: + # See https://github.com/pytest-dev/pytest/issues/11714 + result = yield + try: + if test_name not in self.return_value: + # Test function did not complete successfully + summary['status'] = 'failed' + summary['status_msg'] = ('Test function raised an exception ' + 'before returning a figure.') self._test_results[test_name] = summary - pytest.fail(msg, pytrace=False) + return + fig = self.return_value[test_name] + + if remove_text: + remove_ticks_and_titles(fig) + + result_dir = self.make_test_results_dir(item) + + # What we do now depends on whether we are generating the + # reference images or simply running the test. + if self.generate_dir is not None: + summary['status'] = 'skipped' + summary['image_status'] = 'generated' + summary['status_msg'] = 'Skipped test, since generating image.' + generate_image = self.generate_baseline_image(item, fig) + if self.results_always: # Make baseline image available in HTML + result_image = (result_dir / f"baseline.{ext}").absolute() + shutil.copy(generate_image, result_image) + summary['baseline_image'] = \ + result_image.relative_to(self.results_dir).as_posix() + + if self.generate_hash_library is not None: + summary['hash_status'] = 'generated' + image_hash = self.generate_image_hash(item, fig) + self._generated_hash_library[test_name] = image_hash + summary['baseline_hash'] = image_hash + + # Only test figures if not generating images + if self.generate_dir is None: + # Compare to hash library + if self.hash_library or compare.kwargs.get('hash_library', None): + msg = self.compare_image_to_hash_library(item, fig, result_dir, summary=summary) + + # Compare against a baseline if specified + else: + msg = self.compare_image_to_baseline(item, fig, result_dir, summary=summary) + + close_mpl_figure(fig) + + if msg is None: + if not self.results_always: + shutil.rmtree(result_dir) + for image_type in ['baseline_image', 'diff_image', 'result_image']: + summary[image_type] = None # image no longer exists + else: + self._test_results[test_name] = summary + pytest.fail(msg, pytrace=False) - close_mpl_figure(fig) + close_mpl_figure(fig) - self._test_results[test_name] = summary + self._test_results[test_name] = summary - if summary['status'] == 'skipped': - pytest.skip(summary['status_msg']) + if summary['status'] == 'skipped': + pytest.skip(summary['status_msg']) + except BaseException as e: + if hasattr(result, "force_exception"): # pluggy>=1.2.0 + result.force_exception(e) + else: + result._result = None + result._excinfo = (type(e), e, e.__traceback__) def generate_summary_json(self): json_file = self.results_dir / 'results.json' diff --git a/setup.cfg b/setup.cfg index f8026f9..a874af3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -55,11 +55,11 @@ filterwarnings = ignore:The NumPy module was reloaded [flake8] -max-line-length = 100 -ignore = W504 +max-line-length = 120 +ignore = W503,W504 [pycodestyle] -max_line_length = 100 +max_line_length = 120 [isort] balanced_wrapping = True