Skip to content

Commit d0f8f81

Browse files
authored
Merge pull request #19179 from dstansby/test_fig_check
Check that no new figures are created in image comparison tests
2 parents 7b4510f + b1b6bea commit d0f8f81

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

lib/matplotlib/testing/decorators.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,12 @@ def check_figures_equal(*, extensions=("png", "pdf", "svg"), tol=0):
408408
tol : float
409409
The RMS threshold above which the test is considered failed.
410410
411+
Raises
412+
------
413+
RuntimeError
414+
If any new figures are created (and not subsequently closed) inside
415+
the test function.
416+
411417
Examples
412418
--------
413419
Check that calling `.Axes.plot` with a single argument plots it against
@@ -421,6 +427,7 @@ def test_plot(fig_test, fig_ref):
421427
"""
422428
ALLOWED_CHARS = set(string.digits + string.ascii_letters + '_-[]()')
423429
KEYWORD_ONLY = inspect.Parameter.KEYWORD_ONLY
430+
424431
def decorator(func):
425432
import pytest
426433

@@ -444,7 +451,16 @@ def wrapper(*args, ext, request, **kwargs):
444451
try:
445452
fig_test = plt.figure("test")
446453
fig_ref = plt.figure("reference")
454+
# Keep track of number of open figures, to make sure test
455+
# doesn't create any new ones
456+
n_figs = len(plt.get_fignums())
447457
func(*args, fig_test=fig_test, fig_ref=fig_ref, **kwargs)
458+
if len(plt.get_fignums()) > n_figs:
459+
raise RuntimeError('Number of open figures changed during '
460+
'test. Make sure you are plotting to '
461+
'fig_test or fig_ref, or if this is '
462+
'deliberate explicitly close the '
463+
'new figure(s) inside the test.')
448464
test_image_path = result_dir / (file_name + "." + ext)
449465
ref_image_path = result_dir / (file_name + "-expected." + ext)
450466
fig_test.savefig(test_image_path)

lib/matplotlib/tests/test_axes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ def test_acorr(fig_test, fig_ref):
109109
x = np.random.normal(0, 1, Nx).cumsum()
110110
maxlags = Nx-1
111111

112-
fig_test, ax_test = plt.subplots()
112+
ax_test = fig_test.subplots()
113113
ax_test.acorr(x, maxlags=maxlags)
114114

115-
fig_ref, ax_ref = plt.subplots()
115+
ax_ref = fig_ref.subplots()
116116
# Normalized autocorrelation
117117
norm_auto_corr = np.correlate(x, x, mode="full")/np.dot(x, x)
118118
lags = np.arange(-maxlags, maxlags+1)

lib/matplotlib/tests/test_testing.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import warnings
2+
23
import pytest
4+
5+
import matplotlib.pyplot as plt
36
from matplotlib.testing.decorators import check_figures_equal
47

58

@@ -22,3 +25,17 @@ def test_wrap_failure():
2225
@check_figures_equal()
2326
def should_fail(test, ref):
2427
pass
28+
29+
30+
@pytest.mark.xfail(raises=RuntimeError, strict=True,
31+
reason='Test for check_figures_equal test creating '
32+
'new figures')
33+
@check_figures_equal()
34+
def test_check_figures_equal_extra_fig(fig_test, fig_ref):
35+
plt.figure()
36+
37+
38+
@check_figures_equal()
39+
def test_check_figures_equal_closed_fig(fig_test, fig_ref):
40+
fig = plt.figure()
41+
plt.close(fig)

0 commit comments

Comments
 (0)