Skip to content

Commit 3322808

Browse files
committed
TST: Simplify image testing decorator calls
If a single extension is being tested, then as a shortcut, it can be specified in the basename directly. Also, the default for `check_figures_equal` is to test only `.png` now, so no need to specify that.
1 parent 78c9f7e commit 3322808

File tree

11 files changed

+51
-60
lines changed

11 files changed

+51
-60
lines changed

doc/devel/testing.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ tests it::
153153
fig, ax = plt.subplots()
154154
ax.plot(range(10), linestyle=(0, (3, 3)), lw=5)
155155

156+
If you wish to compare only a single extension, then that may be included in the
157+
baseline image name as a shortcut::
158+
159+
@image_comparison(baseline_images=['line_dashes.png'], remove_text=True,
160+
style='mpl20')
161+
def test_line_dashes():
162+
fig, ax = plt.subplots()
163+
ax.plot(range(10), linestyle=(0, (3, 3)), lw=5)
164+
156165
The first time this test is run, there will be no baseline image to compare
157166
against, so the test will fail. Copy the output images (in this case
158167
:file:`result_images/test_lines/test_line_dashes.png`) to the correct

lib/matplotlib/tests/test_axes.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -919,8 +919,7 @@ def test_structured_data():
919919
axs[1].plot("ones", "twos", "r", data=pts)
920920

921921

922-
@image_comparison(['aitoff_proj'], extensions=["png"],
923-
remove_text=True, style='mpl20')
922+
@image_comparison(['aitoff_proj.png'], remove_text=True, style='mpl20')
924923
def test_aitoff_proj():
925924
"""
926925
Test aitoff projection ref.:
@@ -1410,7 +1409,7 @@ def test_pcolormesh():
14101409
ax3.pcolormesh(Qx, Qz, Zm, shading="gouraud")
14111410

14121411

1413-
@image_comparison(['pcolormesh_small'], extensions=["eps"])
1412+
@image_comparison(['pcolormesh_small.eps'])
14141413
def test_pcolormesh_small():
14151414
n = 3
14161415
x = np.linspace(-1.5, 1.5, n)
@@ -2915,7 +2914,7 @@ def test_scatter_marker(self):
29152914
edgecolors=['k', 'r', 'g', 'b'],
29162915
marker=verts)
29172916

2918-
@image_comparison(['scatter_2D'], remove_text=True, extensions=['png'])
2917+
@image_comparison(['scatter_2D.png'], remove_text=True)
29192918
def test_scatter_2D(self):
29202919
x = np.arange(3)
29212920
y = np.arange(2)
@@ -5683,8 +5682,7 @@ def test_axline_args():
56835682
plt.draw()
56845683

56855684

5686-
@image_comparison(['vlines_basic', 'vlines_with_nan', 'vlines_masked'],
5687-
extensions=['png'])
5685+
@image_comparison(['vlines_basic.png', 'vlines_with_nan.png', 'vlines_masked.png'])
56885686
def test_vlines():
56895687
# normal
56905688
x1 = [2, 3, 4, 5, 7]
@@ -5730,8 +5728,7 @@ def test_vlines_default():
57305728
assert mpl.colors.same_color(lines.get_color(), 'red')
57315729

57325730

5733-
@image_comparison(['hlines_basic', 'hlines_with_nan', 'hlines_masked'],
5734-
extensions=['png'])
5731+
@image_comparison(['hlines_basic.png', 'hlines_with_nan.png', 'hlines_masked.png'])
57355732
def test_hlines():
57365733
# normal
57375734
y1 = [2, 3, 4, 5, 7]
@@ -5795,8 +5792,7 @@ def test_lines_with_colors(fig_test, fig_ref, data):
57955792
colors=expect_color, linewidth=5)
57965793

57975794

5798-
@image_comparison(['vlines_hlines_blended_transform'],
5799-
extensions=['png'], style='mpl20')
5795+
@image_comparison(['vlines_hlines_blended_transform.png'], style='mpl20')
58005796
def test_vlines_hlines_blended_transform():
58015797
t = np.arange(5.0, 10.0, 0.1)
58025798
s = np.exp(-t) + np.sin(2 * np.pi * t) + 10
@@ -6492,8 +6488,8 @@ def test_pie_default():
64926488
autopct='%1.1f%%', shadow=True, startangle=90)
64936489

64946490

6495-
@image_comparison(['pie_linewidth_0', 'pie_linewidth_0', 'pie_linewidth_0'],
6496-
extensions=['png'], style='mpl20', tol=0.01)
6491+
@image_comparison(['pie_linewidth_0.png', 'pie_linewidth_0.png', 'pie_linewidth_0.png'],
6492+
style='mpl20', tol=0.01)
64976493
def test_pie_linewidth_0():
64986494
# The slices will be ordered and plotted counter-clockwise.
64996495
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'

lib/matplotlib/tests/test_bbox_tight.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ def test_noop_tight_bbox():
167167
assert im.shape == (7, 10, 4)
168168

169169

170-
@image_comparison(['bbox_inches_fixed_aspect'], extensions=['png'],
171-
remove_text=True, savefig_kwarg={'bbox_inches': 'tight'})
170+
@image_comparison(['bbox_inches_fixed_aspect.png'], remove_text=True,
171+
savefig_kwarg={'bbox_inches': 'tight'})
172172
def test_bbox_inches_fixed_aspect():
173173
with plt.rc_context({'figure.constrained_layout.use': True}):
174174
fig, ax = plt.subplots()
@@ -177,9 +177,8 @@ def test_bbox_inches_fixed_aspect():
177177
ax.set_aspect('equal')
178178

179179

180-
@image_comparison(['bbox_inches_inset_rasterized'], extensions=['pdf'],
181-
remove_text=True, savefig_kwarg={'bbox_inches': 'tight'},
182-
style='mpl20')
180+
@image_comparison(['bbox_inches_inset_rasterized.pdf'], remove_text=True,
181+
savefig_kwarg={'bbox_inches': 'tight'}, style='mpl20')
183182
def test_bbox_inches_inset_rasterized():
184183
fig, ax = plt.subplots()
185184

lib/matplotlib/tests/test_collections.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ def test_polycollection_close():
491491
ax.set_ylim3d(0, 4)
492492

493493

494-
@check_figures_equal(extensions=["png"])
494+
@check_figures_equal()
495495
def test_scalarmap_change_cmap(fig_test, fig_ref):
496496
# Ensure that changing the colormap of a 3D scatter after draw updates the colors.
497497

@@ -1314,8 +1314,7 @@ def test_set_offset_units():
13141314
np.testing.assert_allclose(off0, sc.get_offsets())
13151315

13161316

1317-
@image_comparison(baseline_images=["test_check_masked_offsets"],
1318-
extensions=["png"], remove_text=True, style="mpl20")
1317+
@image_comparison(["test_check_masked_offsets.png"], remove_text=True, style="mpl20")
13191318
def test_check_masked_offsets():
13201319
# Check if masked data is respected by scatter
13211320
# Ref: Issue #24545

lib/matplotlib/tests/test_colorbar.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,12 @@ def test_colorbar_extension_inverted_axis(orientation, extend, expected):
154154

155155
# TODO: tighten tolerance after baseline image is regenerated for text overhaul
156156
@pytest.mark.parametrize('use_gridspec', [True, False])
157-
@image_comparison(['cbar_with_orientation',
158-
'cbar_locationing',
159-
'double_cbar',
160-
'cbar_sharing',
157+
@image_comparison(['cbar_with_orientation.png',
158+
'cbar_locationing.png',
159+
'double_cbar.png',
160+
'cbar_sharing.png',
161161
],
162-
extensions=['png'], remove_text=True,
163-
savefig_kwarg={'dpi': 40}, tol=0.05)
162+
remove_text=True, savefig_kwarg={'dpi': 40}, tol=0.05)
164163
def test_colorbar_positioning(use_gridspec):
165164
# Remove this line when this test image is regenerated.
166165
plt.rcParams['pcolormesh.snap'] = False

lib/matplotlib/tests/test_colors.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,8 +847,7 @@ def test_cmap_and_norm_from_levels_and_colors():
847847
ax.tick_params(labelleft=False, labelbottom=False)
848848

849849

850-
@image_comparison(baseline_images=['boundarynorm_and_colorbar'],
851-
extensions=['png'], tol=1.0)
850+
@image_comparison(['boundarynorm_and_colorbar.png'], tol=1.0)
852851
def test_boundarynorm_and_colorbarbase():
853852
# Remove this line when this test image is regenerated.
854853
plt.rcParams['pcolormesh.snap'] = False

lib/matplotlib/tests/test_contour.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ def test_contour_manual_moveto():
128128

129129

130130
# TODO: tighten tolerance after baseline image is regenerated for text overhaul
131-
@image_comparison(['contour_disconnected_segments'],
132-
remove_text=True, style='mpl20', extensions=['png'], tol=0.01)
131+
@image_comparison(['contour_disconnected_segments.png'],
132+
remove_text=True, style='mpl20', tol=0.01)
133133
def test_contour_label_with_disconnected_segments():
134134
x, y = np.mgrid[-1:1:21j, -1:1:21j]
135135
z = 1 / np.sqrt(0.01 + (x + 0.3) ** 2 + y ** 2)
@@ -172,8 +172,7 @@ def test_given_colors_levels_and_extends():
172172
plt.colorbar(c, ax=ax)
173173

174174

175-
@image_comparison(['contourf_hatch_colors'],
176-
remove_text=True, style='mpl20', extensions=['png'])
175+
@image_comparison(['contourf_hatch_colors.png'], remove_text=True, style='mpl20')
177176
def test_hatch_colors():
178177
fig, ax = plt.subplots()
179178
cf = ax.contourf([[0, 1], [1, 2]], hatches=['-', '/', '\\', '//'], cmap='gray')
@@ -455,8 +454,7 @@ def test_contour_addlines():
455454
assert_array_almost_equal(cb.ax.get_ylim(), [114.3091, 9972.30735], 3)
456455

457456

458-
@image_comparison(baseline_images=['contour_uneven'],
459-
extensions=['png'], remove_text=True, style='mpl20')
457+
@image_comparison(['contour_uneven.png'], remove_text=True, style='mpl20')
460458
def test_contour_uneven():
461459
# Remove this line when this test image is regenerated.
462460
plt.rcParams['pcolormesh.snap'] = False
@@ -494,8 +492,7 @@ def test_label_nonagg():
494492
plt.clabel(plt.contour([[1, 2], [3, 4]]))
495493

496494

497-
@image_comparison(baseline_images=['contour_closed_line_loop'],
498-
extensions=['png'], remove_text=True)
495+
@image_comparison(['contour_closed_line_loop.png'], remove_text=True)
499496
def test_contour_closed_line_loop():
500497
# github issue 19568.
501498
z = [[0, 0, 0], [0, 2, 0], [0, 0, 0], [2, 1, 2]]
@@ -519,8 +516,7 @@ def test_quadcontourset_reuse():
519516
assert qcs3._contour_generator == qcs1._contour_generator
520517

521518

522-
@image_comparison(baseline_images=['contour_manual'],
523-
extensions=['png'], remove_text=True, tol=0.89)
519+
@image_comparison(['contour_manual.png'], remove_text=True, tol=0.89)
524520
def test_contour_manual():
525521
# Manually specifying contour lines/polygons to plot.
526522
from matplotlib.contour import ContourSet
@@ -545,8 +541,7 @@ def test_contour_manual():
545541
ContourSet(ax, [2], [segs], [kinds], colors='k', linewidths=3)
546542

547543

548-
@image_comparison(baseline_images=['contour_line_start_on_corner_edge'],
549-
extensions=['png'], remove_text=True)
544+
@image_comparison(['contour_line_start_on_corner_edge.png'], remove_text=True)
550545
def test_contour_line_start_on_corner_edge():
551546
fig, ax = plt.subplots(figsize=(6, 5))
552547

@@ -680,8 +675,7 @@ def test_algorithm_supports_corner_mask(algorithm):
680675
plt.contourf(z, algorithm=algorithm, corner_mask=True)
681676

682677

683-
@image_comparison(baseline_images=['contour_all_algorithms'],
684-
extensions=['png'], remove_text=True, tol=0.06)
678+
@image_comparison(['contour_all_algorithms.png'], remove_text=True, tol=0.06)
685679
def test_all_algorithms():
686680
algorithms = ['mpl2005', 'mpl2014', 'serial', 'threaded']
687681

@@ -858,8 +852,8 @@ def test_allsegs_allkinds():
858852
assert len(result[1]) == 4
859853

860854

861-
@image_comparison(baseline_images=['contour_rasterization'],
862-
extensions=['pdf'], style='mpl20', savefig_kwarg={'dpi': 25})
855+
@image_comparison(['contour_rasterization.pdf'], savefig_kwarg={'dpi': 25},
856+
style='mpl20')
863857
def test_contourf_rasterize():
864858
fig, ax = plt.subplots()
865859
data = [[0, 1], [1, 0]]
@@ -868,7 +862,7 @@ def test_contourf_rasterize():
868862
assert cs._rasterized
869863

870864

871-
@check_figures_equal(extensions=["png"])
865+
@check_figures_equal()
872866
def test_contour_aliases(fig_test, fig_ref):
873867
data = np.arange(100).reshape((10, 10)) ** 2
874868
fig_test.add_subplot().contour(data, linestyle=":")

lib/matplotlib/tests/test_patches.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ def test_boxstyle_errors(fmt, match):
813813
BoxStyle(fmt)
814814

815815

816-
@image_comparison(baseline_images=['annulus'], extensions=['png'])
816+
@image_comparison(['annulus.png'])
817817
def test_annulus():
818818

819819
fig, ax = plt.subplots()
@@ -825,7 +825,7 @@ def test_annulus():
825825
ax.set_aspect('equal')
826826

827827

828-
@image_comparison(baseline_images=['annulus'], extensions=['png'])
828+
@image_comparison(['annulus.png'])
829829
def test_annulus_setters():
830830

831831
fig, ax = plt.subplots()
@@ -846,7 +846,7 @@ def test_annulus_setters():
846846
ell.angle = 45
847847

848848

849-
@image_comparison(baseline_images=['annulus'], extensions=['png'])
849+
@image_comparison(['annulus.png'])
850850
def test_annulus_setters2():
851851

852852
fig, ax = plt.subplots()

lib/matplotlib/tests/test_text.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,7 @@ def test_empty_annotation_get_window_extent():
11361136

11371137

11381138
# TODO: tighten tolerance after baseline image is regenerated for text overhaul
1139-
@image_comparison(baseline_images=['basictext_wrap'],
1140-
extensions=['png'], tol=0.3)
1139+
@image_comparison(['basictext_wrap.png'], tol=0.3)
11411140
def test_basic_wrap():
11421141
fig = plt.figure()
11431142
plt.axis([0, 10, 0, 10])
@@ -1154,8 +1153,7 @@ def test_basic_wrap():
11541153

11551154

11561155
# TODO: tighten tolerance after baseline image is regenerated for text overhaul
1157-
@image_comparison(baseline_images=['fonttext_wrap'],
1158-
extensions=['png'], tol=0.3)
1156+
@image_comparison(['fonttext_wrap.png'], tol=0.3)
11591157
def test_font_wrap():
11601158
fig = plt.figure()
11611159
plt.axis([0, 10, 0, 10])
@@ -1188,8 +1186,8 @@ def test_va_for_angle():
11881186

11891187

11901188
# TODO: tighten tolerance after baseline image is regenerated for text overhaul
1191-
@image_comparison(baseline_images=['xtick_rotation_mode'],
1192-
remove_text=False, extensions=['png'], style='mpl20', tol=0.3)
1189+
@image_comparison(['xtick_rotation_mode.png'], remove_text=False, style='mpl20',
1190+
tol=0.3)
11931191
def test_xtick_rotation_mode():
11941192
fig, ax = plt.subplots(figsize=(12, 1))
11951193
ax.set_yticks([])
@@ -1209,8 +1207,8 @@ def test_xtick_rotation_mode():
12091207

12101208

12111209
# TODO: tighten tolerance after baseline image is regenerated for text overhaul
1212-
@image_comparison(baseline_images=['ytick_rotation_mode'],
1213-
remove_text=False, extensions=['png'], style='mpl20', tol=0.3)
1210+
@image_comparison(['ytick_rotation_mode.png'], remove_text=False, style='mpl20',
1211+
tol=0.3)
12141212
def test_ytick_rotation_mode():
12151213
fig, ax = plt.subplots(figsize=(1, 12))
12161214
ax.set_xticks([])

lib/mpl_toolkits/axes_grid1/tests/test_axes_grid1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_divider_append_axes():
6262

6363

6464
# Update style when regenerating the test image
65-
@image_comparison(['twin_axes_empty_and_removed'], extensions=["png"], tol=1,
65+
@image_comparison(['twin_axes_empty_and_removed.png'], tol=1,
6666
style=('classic', '_classic_test_patch'))
6767
def test_twin_axes_empty_and_removed():
6868
# Purely cosmetic font changes (avoid overlap)

0 commit comments

Comments
 (0)