9
9
10
10
11
11
@pytest .fixture (autouse = True )
12
- def mpl_test_settings (qt5_module , mpl_test_settings ):
12
+ def mpl_test_settings (qt_module , mpl_test_settings ):
13
13
"""
14
- Ensure qt5_module fixture is *first* fixture.
14
+ Ensure qt_module fixture is *first* fixture.
15
15
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 `
17
17
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
19
19
`mpl_test_settings` fixture will try to switch backends before the skip can
20
20
be triggered.
21
21
"""
22
22
pass
23
23
24
24
25
25
@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' :
31
29
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 )
35
48
36
49
qt_compat = pytest .importorskip ('matplotlib.backends.qt_compat' )
37
50
QtCore = qt_compat .QtCore
38
51
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 )
41
66
42
67
mods = {}
43
68
keys = {}
@@ -52,7 +77,7 @@ def qt5_module():
52
77
53
78
@pytest .fixture
54
79
def qt_key (request ):
55
- QtCore , _ , keys = request .getfixturevalue ('qt5_module ' )
80
+ QtCore , _ , keys = request .getfixturevalue ('qt_module ' )
56
81
if request .param .startswith ('Key' ):
57
82
return getattr (QtCore .Qt , request .param )
58
83
else :
@@ -61,15 +86,19 @@ def qt_key(request):
61
86
62
87
@pytest .fixture
63
88
def qt_mods (request ):
64
- QtCore , mods , _ = request .getfixturevalue ('qt5_module ' )
89
+ QtCore , mods , _ = request .getfixturevalue ('qt_module ' )
65
90
result = QtCore .Qt .NoModifier
66
91
for mod in request .param :
67
92
result |= mods [mod ]
68
93
return result
69
94
70
95
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 ):
73
102
# save the state of Gcf.figs
74
103
init_figs = copy .copy (Gcf .figs )
75
104
@@ -118,11 +147,15 @@ def test_fig_close():
118
147
'non_unicode_key' ,
119
148
]
120
149
)
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 ):
123
156
"""
124
157
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)
126
159
Catch the event
127
160
Assert sent and caught keys are the same
128
161
"""
0 commit comments