Skip to content

Commit d70a834

Browse files
committed
Make sure running the tests without --mpl still results in figures getting closed to avoid memory issues.
1 parent 78ab284 commit d70a834

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ env:
2121
- PYTHON_VERSION=3.6 MATPLOTLIB_VERSION=2.0
2222

2323
# The following build is meant to check that dependencies get set up correctly, but currently
24-
# the image tests fail, which should be investigated.
24+
# the image tests fail, which should be investigated.
2525
# - PYTHON_VERSION=3.5 CONDA_DEPENDENCIES="coverage freetype libpng"
2626

2727
install:
@@ -37,6 +37,9 @@ install:
3737
script:
3838
- python -c 'import pytest_mpl.plugin'
3939
- pytest -vv --mpl --cov pytest_mpl tests
40+
# Make sure that the tests run ok even without the --mpl option (we close
41+
# figures anyway in this case)
42+
- pytest -vv --cov pytest_mpl --cov-append tests
4043
- python setup.py check --restructuredtext
4144

4245
after_success:

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
- Allow baseline_dir to be comma-separated URL list to allow mirrors to
55
be specified. [#59]
66

7+
- Make sure figures get closed even if not running with the --mpl option.
8+
79
0.8 (2017-07-19)
810
----------------
911

pytest_mpl/plugin.py

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import os
3535
import sys
3636
import shutil
37+
import inspect
3738
import tempfile
3839
import warnings
3940
from distutils.version import LooseVersion
@@ -42,7 +43,7 @@
4243

4344
if sys.version_info[0] == 2:
4445
from urllib import urlopen
45-
string_types = basestring
46+
string_types = basestring # noqa
4647
else:
4748
from urllib.request import urlopen
4849
string_types = str
@@ -70,7 +71,7 @@ def _download_file(baseline, filename):
7071

7172

7273
def 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
122127
def 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

Comments
 (0)