Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions lib/matplotlib/testing/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/matplotlib/tests/test_backend_cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
9 changes: 5 additions & 4 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
4 changes: 3 additions & 1 deletion lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same underlying problem as with cairo, because CanvasBase does not have the ability to draw anything (and if I recall this warning comes out of Agg) so this forces Agg to be used to render.

# Write to a buffer to trigger the warning
fig_test.savefig(buffer)

array = np.zeros((1, size // 2 + 1))
array[:, array.size // 2:] = 1
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/tests/test_polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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(),
Expand Down
16 changes: 5 additions & 11 deletions lib/matplotlib/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import pytest

import matplotlib.pyplot as plt
from matplotlib.testing.decorators import check_figures_equal


Expand All @@ -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


Expand All @@ -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
Loading