@@ -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 )
0 commit comments