99
1010
1111@pytest .fixture (autouse = True )
12- def mpl_test_settings (qt5_module , mpl_test_settings ):
12+ def mpl_test_settings (qt_module , mpl_test_settings ):
1313 """
14- Ensure qt5_module fixture is *first* fixture.
14+ Ensure qt_module fixture is *first* fixture.
1515
16- We override the `mpl_test_settings` fixture and depend on the `qt5_module `
16+ We override the `mpl_test_settings` fixture and depend on the `qt_module `
1717 fixture first. It is very important that it is first, because it skips
18- tests when Qt5 is not available, and if not, then the main
18+ tests when Qt is not available, and if not, then the main
1919 `mpl_test_settings` fixture will try to switch backends before the skip can
2020 be triggered.
2121 """
2222 pass
2323
2424
2525@pytest .fixture
26- def qt5_module ():
27- try :
28- import PyQt5
29- # RuntimeError if PyQt4 already imported.
30- except (ImportError , RuntimeError ):
26+ def qt_module (request ):
27+ backend , = request .node .get_closest_marker ('backend' ).args
28+ if backend == 'Qt4Agg' :
3129 try :
32- import PySide2
33- except ImportError :
34- pytest .skip ("Failed to import a Qt5 binding." )
30+ import PyQt4
31+ # RuntimeError if PyQt5 already imported.
32+ except (ImportError , RuntimeError ):
33+ try :
34+ import PySide
35+ except ImportError :
36+ pytest .skip ("Failed to import a Qt4 binding." )
37+ elif backend == 'Qt5Agg' :
38+ try :
39+ import PyQt5
40+ # RuntimeError if PyQt4 already imported.
41+ except (ImportError , RuntimeError ):
42+ try :
43+ import PySide2
44+ except ImportError :
45+ pytest .skip ("Failed to import a Qt5 binding." )
46+ else :
47+ raise ValueError ('Backend marker has unknown value: ' + backend )
3548
3649 qt_compat = pytest .importorskip ('matplotlib.backends.qt_compat' )
3750 QtCore = qt_compat .QtCore
3851
39- from matplotlib .backends .backend_qt5 import (
40- MODIFIER_KEYS , SUPER , ALT , CTRL , SHIFT ) # noqa
52+ if backend == 'Qt4Agg' :
53+ try :
54+ py_qt_ver = int (QtCore .PYQT_VERSION_STR .split ('.' )[0 ])
55+ except AttributeError :
56+ py_qt_ver = QtCore .__version_info__ [0 ]
57+
58+ if py_qt_ver != 4 :
59+ pytest .skip (reason = 'Qt4 is not available' )
60+
61+ from matplotlib .backends .backend_qt4 import (
62+ MODIFIER_KEYS , SUPER , ALT , CTRL , SHIFT )
63+ elif backend == 'Qt5Agg' :
64+ from matplotlib .backends .backend_qt5 import (
65+ MODIFIER_KEYS , SUPER , ALT , CTRL , SHIFT )
4166
4267 mods = {}
4368 keys = {}
@@ -52,7 +77,7 @@ def qt5_module():
5277
5378@pytest .fixture
5479def qt_key (request ):
55- QtCore , _ , keys = request .getfixturevalue ('qt5_module ' )
80+ QtCore , _ , keys = request .getfixturevalue ('qt_module ' )
5681 if request .param .startswith ('Key' ):
5782 return getattr (QtCore .Qt , request .param )
5883 else :
@@ -61,15 +86,19 @@ def qt_key(request):
6186
6287@pytest .fixture
6388def qt_mods (request ):
64- QtCore , mods , _ = request .getfixturevalue ('qt5_module ' )
89+ QtCore , mods , _ = request .getfixturevalue ('qt_module ' )
6590 result = QtCore .Qt .NoModifier
6691 for mod in request .param :
6792 result |= mods [mod ]
6893 return result
6994
7095
71- @pytest .mark .backend ('Qt5Agg' )
72- def test_fig_close ():
96+ @pytest .mark .parametrize ('backend' , [
97+ # Note: the value is irrelevant; the important part is the marker.
98+ pytest .param ('Qt4Agg' , marks = pytest .mark .backend ('Qt4Agg' )),
99+ pytest .param ('Qt5Agg' , marks = pytest .mark .backend ('Qt5Agg' )),
100+ ])
101+ def test_fig_close (backend ):
73102 # save the state of Gcf.figs
74103 init_figs = copy .copy (Gcf .figs )
75104
@@ -118,11 +147,15 @@ def test_fig_close():
118147 'non_unicode_key' ,
119148 ]
120149)
121- @pytest .mark .backend ('Qt5Agg' )
122- def test_correct_key (qt_key , qt_mods , answer ):
150+ @pytest .mark .parametrize ('backend' , [
151+ # Note: the value is irrelevant; the important part is the marker.
152+ pytest .param ('Qt4Agg' , marks = pytest .mark .backend ('Qt4Agg' )),
153+ pytest .param ('Qt5Agg' , marks = pytest .mark .backend ('Qt5Agg' )),
154+ ])
155+ def test_correct_key (backend , qt_key , qt_mods , answer ):
123156 """
124157 Make a figure
125- Send a key_press_event event (using non-public, qt5 backend specific api)
158+ Send a key_press_event event (using non-public, qtX backend specific api)
126159 Catch the event
127160 Assert sent and caught keys are the same
128161 """
0 commit comments