diff --git a/lib/matplotlib/testing/decorators.py b/lib/matplotlib/testing/decorators.py index 7e9621cb3b1d..c258574dca11 100644 --- a/lib/matplotlib/testing/decorators.py +++ b/lib/matplotlib/testing/decorators.py @@ -389,18 +389,22 @@ def decorator(func): @pytest.mark.parametrize("ext", extensions) def wrapper(*args, ext, **kwargs): - fig_test = plt.figure("test") - fig_ref = plt.figure("reference") - func(*args, fig_test=fig_test, fig_ref=fig_ref, **kwargs) - test_image_path = result_dir / (func.__name__ + "." + ext) - ref_image_path = result_dir / ( - func.__name__ + "-expected." + ext - ) - fig_test.savefig(test_image_path) - fig_ref.savefig(ref_image_path) - _raise_on_image_difference( - ref_image_path, test_image_path, tol=tol - ) + try: + fig_test = plt.figure("test") + fig_ref = plt.figure("reference") + func(*args, fig_test=fig_test, fig_ref=fig_ref, **kwargs) + test_image_path = result_dir / (func.__name__ + "." + ext) + ref_image_path = result_dir / ( + func.__name__ + "-expected." + ext + ) + fig_test.savefig(test_image_path) + fig_ref.savefig(ref_image_path) + _raise_on_image_difference( + ref_image_path, test_image_path, tol=tol + ) + finally: + plt.close(fig_test) + plt.close(fig_ref) sig = inspect.signature(func) new_sig = sig.replace(