Skip to content

Commit

Permalink
Report if test function raised an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
ConorMacBride committed Apr 3, 2023
1 parent 24ba2aa commit 8a0ec45
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions pytest_mpl/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ def compare_image_to_hash_library(self, item, fig, result_dir, summary=None):
baseline_comparison = self.compare_image_to_baseline(item, fig, result_dir,
summary=baseline_summary)
except Exception as baseline_error: # Append to test error later
summary['image_status'] = 'diff' # (not necessarily diff, but makes user aware)
baseline_comparison = str(baseline_error)
else: # Update main summary
for k in ['image_status', 'baseline_image', 'diff_image',
Expand Down Expand Up @@ -697,25 +698,14 @@ def pytest_runtest_call(self, item): # noqa

with plt.style.context(style, after_reset=True), switch_backend(backend):

# Run test and get figure object
wrap_figure_interceptor(self, item)
yield
test_name = generate_test_name(item)
if test_name not in self.return_value:
# Test function did not complete successfully
return
fig = self.return_value[test_name]

if remove_text:
remove_ticks_and_titles(fig)

result_dir = self.make_test_results_dir(item)

# Store fallback summary in case of exceptions
summary = {
'status': None,
'status': 'failed',
'image_status': None,
'hash_status': None,
'status_msg': None,
'status_msg': 'An exception was raised while testing the figure.',
'baseline_image': None,
'diff_image': None,
'rms': None,
Expand All @@ -724,6 +714,24 @@ def pytest_runtest_call(self, item): # noqa
'baseline_hash': None,
'result_hash': None,
}
self._test_results[test_name] = summary

# 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.
Expand Down

0 comments on commit 8a0ec45

Please sign in to comment.