3434import os
3535import sys
3636import shutil
37+ import inspect
3738import tempfile
3839import warnings
3940from distutils .version import LooseVersion
4243
4344if sys .version_info [0 ] == 2 :
4445 from urllib import urlopen
45- string_types = basestring
46+ string_types = basestring # noqa
4647else :
4748 from urllib .request import urlopen
4849 string_types = str
@@ -70,7 +71,7 @@ def _download_file(baseline, filename):
7071
7172
7273def pytest_addoption (parser ):
73- group = parser .getgroup ("general " )
74+ group = parser .getgroup ("matplotlib image comparison " )
7475 group .addoption ('--mpl' , action = 'store_true' ,
7576 help = "Enable comparison of matplotlib figures to reference files" )
7677 group .addoption ('--mpl-generate-path' ,
@@ -117,6 +118,10 @@ def pytest_configure(config):
117118 generate_dir = generate_dir ,
118119 results_dir = results_dir ))
119120
121+ else :
122+
123+ config .pluginmanager .register (FigureCloser (config ))
124+
120125
121126@contextlib .contextmanager
122127def switch_backend (backend ):
@@ -143,6 +148,11 @@ def __init__(self, config, baseline_dir=None, generate_dir=None, results_dir=Non
143148
144149 def pytest_runtest_setup (self , item ):
145150
151+ compare = item .keywords .get ('mpl_image_compare' )
152+
153+ if compare is None :
154+ return
155+
146156 import matplotlib
147157 import matplotlib .pyplot as plt
148158 from matplotlib .testing .compare import compare_images
@@ -154,11 +164,6 @@ def pytest_runtest_setup(self, item):
154164
155165 MPL_LT_15 = LooseVersion (matplotlib .__version__ ) < LooseVersion ('1.5' )
156166
157- compare = item .keywords .get ('mpl_image_compare' )
158-
159- if compare is None :
160- return
161-
162167 tolerance = compare .kwargs .get ('tolerance' , 2 )
163168 savefig_kwargs = compare .kwargs .get ('savefig_kwargs' , {})
164169 style = compare .kwargs .get ('style' , 'classic' )
@@ -188,7 +193,6 @@ def item_function_wrapper(*args, **kwargs):
188193 with plt .style .context (style ), switch_backend (backend ):
189194
190195 # Run test and get figure object
191- import inspect
192196 if inspect .ismethod (original ): # method
193197 # In some cases, for example if setup_method is used,
194198 # original appears to belong to an instance of the test
@@ -257,3 +261,39 @@ def item_function_wrapper(*args, **kwargs):
257261 setattr (item .cls , item .function .__name__ , item_function_wrapper )
258262 else :
259263 item .obj = item_function_wrapper
264+
265+
266+ class FigureCloser (object ):
267+ """
268+ This is used in place of ImageComparison when the --mpl option is not used,
269+ to make sure that we still close figures returned by tests.
270+ """
271+
272+ def __init__ (self , config ):
273+ self .config = config
274+
275+ def pytest_runtest_setup (self , item ):
276+
277+ compare = item .keywords .get ('mpl_image_compare' )
278+
279+ if compare is None :
280+ return
281+
282+ import matplotlib .pyplot as plt
283+
284+ original = item .function
285+
286+ @wraps (item .function )
287+ def item_function_wrapper (* args , ** kwargs ):
288+
289+ if inspect .ismethod (original ): # method
290+ fig = original .__func__ (* args , ** kwargs )
291+ else : # function
292+ fig = original (* args , ** kwargs )
293+
294+ plt .close (fig )
295+
296+ if item .cls is not None :
297+ setattr (item .cls , item .function .__name__ , item_function_wrapper )
298+ else :
299+ item .obj = item_function_wrapper
0 commit comments