diff --git a/lib/matplotlib/testing/decorators.py b/lib/matplotlib/testing/decorators.py index 17509449e768..b26f3ed4a446 100644 --- a/lib/matplotlib/testing/decorators.py +++ b/lib/matplotlib/testing/decorators.py @@ -15,6 +15,7 @@ import matplotlib.units import matplotlib.testing from matplotlib import _pylab_helpers, cbook, ft2font, pyplot as plt, ticker +from matplotlib.figure import Figure from .compare import comparable_formats, compare_images, make_test_filename from .exceptions import ImageComparisonFailure @@ -410,27 +411,21 @@ def wrapper(*args, ext, request, **kwargs): file_name = "".join(c for c in request.node.name if c in ALLOWED_CHARS) - try: - fig_test = plt.figure("test") - fig_ref = plt.figure("reference") - with _collect_new_figures() as figs: - func(*args, fig_test=fig_test, fig_ref=fig_ref, **kwargs) - if figs: - raise RuntimeError('Number of open figures changed during ' - 'test. Make sure you are plotting to ' - 'fig_test or fig_ref, or if this is ' - 'deliberate explicitly close the ' - 'new figure(s) inside the test.') - test_image_path = result_dir / (file_name + "." + ext) - ref_image_path = result_dir / (file_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) + fig_test = Figure() + fig_ref = Figure() + func(*args, fig_test=fig_test, fig_ref=fig_ref, **kwargs) + if len(fig_test.get_children()) == 1 and len(fig_ref.get_children()) == 1: + # no artists have been added. The only child is fig.patch. + raise RuntimeError("Both figures are empty. Make sure you are " + "plotting to fig_test or fig_ref.") + + test_image_path = result_dir / (file_name + "." + ext) + ref_image_path = result_dir / (file_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 + ) parameters = [ param diff --git a/lib/matplotlib/tests/test_backend_cairo.py b/lib/matplotlib/tests/test_backend_cairo.py index c5712cc50d5b..4eaa8fc1ca3c 100644 --- a/lib/matplotlib/tests/test_backend_cairo.py +++ b/lib/matplotlib/tests/test_backend_cairo.py @@ -2,6 +2,7 @@ import pytest +import matplotlib.pyplot as plt from matplotlib.testing.decorators import check_figures_equal from matplotlib import ( collections as mcollections, patches as mpatches, path as mpath) @@ -46,3 +47,7 @@ def test_patch_alpha_coloring(fig_test, fig_ref): facecolor=(1, 0, 0, 0.5), edgecolor=(0, 0, 1, 0.75)) ax.add_collection(col) + + # Have pyplot manage the figures to ensure the cairo backend is used + plt.figure(fig_ref) + plt.figure(fig_test) diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 72e38d32e82f..23a752bbc3bf 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -896,11 +896,12 @@ def test_twoslope_colorbar(): fig.colorbar(pc) -@check_figures_equal() -def test_remove_cb_whose_mappable_has_no_figure(fig_ref, fig_test): - ax = fig_test.add_subplot() - cb = fig_test.colorbar(cm.ScalarMappable(), cax=ax) +def test_remove_cb_whose_mappable_has_no_figure(): + fig, ax = plt.subplots() + assert fig.get_axes() != [] + cb = fig.colorbar(cm.ScalarMappable(), cax=ax) cb.remove() + assert fig.get_axes() == [] def test_aspects(): diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index da7a198a2a94..d41c38975fda 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -1602,7 +1602,9 @@ def test_large_image(fig_test, fig_ref, dim, size, msg, origin): with pytest.warns(UserWarning, match=f'Data with more than {msg} cannot be ' 'accurately displayed.'): - fig_test.canvas.draw() + with io.BytesIO() as buffer: + # Write to a buffer to trigger the warning + fig_test.savefig(buffer) array = np.zeros((1, size // 2 + 1)) array[:, array.size // 2:] = 1 diff --git a/lib/matplotlib/tests/test_polar.py b/lib/matplotlib/tests/test_polar.py index 4cbb099e3293..f688f384479b 100644 --- a/lib/matplotlib/tests/test_polar.py +++ b/lib/matplotlib/tests/test_polar.py @@ -117,7 +117,7 @@ def test_polar_units_1(fig_test, fig_ref): xs = [30.0, 45.0, 60.0, 90.0] ys = [1.0, 2.0, 3.0, 4.0] - plt.figure(fig_test.number) + plt.figure(fig_test) plt.polar([x * units.deg for x in xs], ys) ax = fig_ref.add_subplot(projection="polar") @@ -134,7 +134,7 @@ def test_polar_units_2(fig_test, fig_ref): ys = [1.0, 2.0, 3.0, 4.0] ys_km = [y * units.km for y in ys] - plt.figure(fig_test.number) + plt.figure(fig_test) # test {theta,r}units. plt.polar(xs_deg, ys_km, thetaunits="rad", runits="km") assert isinstance(plt.gca().xaxis.get_major_formatter(), diff --git a/lib/matplotlib/tests/test_testing.py b/lib/matplotlib/tests/test_testing.py index c438c54d26fa..5c6bbf8ccc61 100644 --- a/lib/matplotlib/tests/test_testing.py +++ b/lib/matplotlib/tests/test_testing.py @@ -2,7 +2,6 @@ import pytest -import matplotlib.pyplot as plt from matplotlib.testing.decorators import check_figures_equal @@ -17,6 +16,8 @@ def test_warn_to_fail(): @check_figures_equal() @pytest.mark.parametrize("b", [1]) def test_parametrize_with_check_figure_equal(a, fig_ref, b, fig_test): + fig_ref.add_subplot() + fig_test.add_subplot() assert a == b @@ -28,14 +29,7 @@ def should_fail(test, ref): @pytest.mark.xfail(raises=RuntimeError, strict=True, - reason='Test for check_figures_equal test creating ' - 'new figures') + reason="Both figures are empty") @check_figures_equal() -def test_check_figures_equal_extra_fig(fig_test, fig_ref): - plt.figure() - - -@check_figures_equal() -def test_check_figures_equal_closed_fig(fig_test, fig_ref): - fig = plt.figure() - plt.close(fig) +def test_check_figures_equal_empty_figs(fig_test, fig_ref): + pass