File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1414import nose
1515import numpy as np
1616
17+ import matplotlib as mpl
1718import matplotlib .tests
1819import matplotlib .units
1920from matplotlib import cbook
@@ -329,3 +330,19 @@ def find_dotted_module(module_name, path=None):
329330 cbook .mkdirs (result_dir )
330331
331332 return baseline_dir , result_dir
333+
334+
335+ def switch_backend (backend ):
336+ def switch_backend_decorator (func ):
337+ def backend_switcher (* args , ** kwargs ):
338+ try :
339+ prev_backend = mpl .get_backend ()
340+ matplotlib .tests .setup ()
341+ plt .switch_backend (backend )
342+ result = func (* args , ** kwargs )
343+ finally :
344+ plt .switch_backend (prev_backend )
345+ return result
346+
347+ return nose .tools .make_decorator (func )(backend_switcher )
348+ return switch_backend_decorator
Original file line number Diff line number Diff line change 1515import matplotlib .pyplot as plt
1616from matplotlib .compat import subprocess
1717from matplotlib .testing .compare import compare_images , ImageComparisonFailure
18- from matplotlib .testing .decorators import _image_directories
18+ from matplotlib .testing .decorators import _image_directories , switch_backend
1919
2020
2121baseline_dir , result_dir = _image_directories (lambda : 'dummy func' )
@@ -41,23 +41,6 @@ def check_for(texsystem):
4141 return latex .returncode == 0
4242
4343
44- def switch_backend (backend ):
45-
46- def switch_backend_decorator (func ):
47- def backend_switcher (* args , ** kwargs ):
48- try :
49- prev_backend = mpl .get_backend ()
50- mpl .rcdefaults ()
51- plt .switch_backend (backend )
52- result = func (* args , ** kwargs )
53- finally :
54- plt .switch_backend (prev_backend )
55- return result
56-
57- return nose .tools .make_decorator (func )(backend_switcher )
58- return switch_backend_decorator
59-
60-
6144def compare_figure (fname , savefig_kwargs = {}):
6245 actual = os .path .join (result_dir , fname )
6346 plt .savefig (actual , ** savefig_kwargs )
Original file line number Diff line number Diff line change 44from matplotlib .externals import six
55from matplotlib .externals .six import unichr
66from matplotlib import pyplot as plt
7- from matplotlib .testing .decorators import cleanup
7+ from matplotlib .testing .decorators import cleanup , switch_backend
88from matplotlib .testing .decorators import knownfailureif
99from matplotlib ._pylab_helpers import Gcf
1010import copy
3131
3232@cleanup
3333@knownfailureif (not HAS_QT )
34+ @switch_backend ('Qt4Agg' )
3435def test_fig_close ():
35- # force switch to the Qt4 backend
36- plt .switch_backend ('Qt4Agg' )
37-
3836 #save the state of Gcf.figs
3937 init_figs = copy .copy (Gcf .figs )
4038
@@ -50,14 +48,14 @@ def test_fig_close():
5048 assert (init_figs == Gcf .figs )
5149
5250
51+ @switch_backend ('Qt4Agg' )
5352def assert_correct_key (qt_key , qt_mods , answer ):
5453 """
5554 Make a figure
5655 Send a key_press_event event (using non-public, qt4 backend specific api)
5756 Catch the event
5857 Assert sent and caught keys are the same
5958 """
60- plt .switch_backend ('Qt4Agg' )
6159 qt_canvas = plt .figure ().canvas
6260
6361 event = mock .Mock ()
You can’t perform that action at this time.
0 commit comments