2
2
from pytestqt .qt_compat import QtGui
3
3
from pytestqt .qt_compat import QtTest
4
4
5
- #===================================================================================================
6
- # QtBot
7
- #===================================================================================================
5
+
6
+ def _inject_qtest_methods (cls ):
7
+ """
8
+ Injects QTest methods into the given class QtBot, so the user can access
9
+ them directly without having to import QTest.
10
+ """
11
+
12
+ def create_qtest_proxy_method (method_name ):
13
+
14
+ def result (* args , ** kwargs ):
15
+ m = getattr (QtTest .QTest , method_name )
16
+ return m (* args , ** kwargs )
17
+
18
+ result .__name__ = method_name
19
+ return staticmethod (result )
20
+
21
+ # inject methods from QTest into QtBot
22
+ method_names = set ([
23
+ 'keyPress' ,
24
+ 'keyClick' ,
25
+ 'keyClicks' ,
26
+ 'keyEvent' ,
27
+ 'keyPress' ,
28
+ 'keyRelease' ,
29
+ 'keyToAscii' ,
30
+
31
+ 'mouseClick' ,
32
+ 'mouseDClick' ,
33
+ 'mouseEvent' ,
34
+ 'mouseMove' ,
35
+ 'mousePress' ,
36
+ 'mouseRelease' ,
37
+ ])
38
+ for method_name in method_names :
39
+ method = create_qtest_proxy_method (method_name )
40
+ setattr (cls , method_name , method )
41
+
42
+ return cls
43
+
44
+
45
+ @_inject_qtest_methods
8
46
class QtBot (object ):
9
- '''
47
+ """
10
48
Instances of this class are responsible for sending events to `Qt` objects (usually widgets),
11
49
simulating user input.
12
50
@@ -103,51 +141,51 @@ class QtBot(object):
103
141
104
142
.. _QTest API: http://doc.qt.digia.com/4.7/qtest.html
105
143
106
- '''
144
+ """
107
145
108
146
def __init__ (self , app ):
109
- '''
147
+ """
110
148
:param QApplication app:
111
149
The current QApplication instance.
112
- '''
150
+ """
113
151
self ._app = app
114
152
self ._widgets = []
115
153
116
154
117
155
def _close (self ):
118
- '''
156
+ """
119
157
Clear up method. Called at the end of each test that uses a ``qtbot`` fixture.
120
- '''
158
+ """
121
159
for w in self ._widgets :
122
160
w .close ()
123
161
self ._widgets [:] = []
124
162
125
163
126
164
def addWidget (self , widget ):
127
- '''
165
+ """
128
166
Adds a widget to be tracked by this bot. This is not required, but will ensure that the
129
167
widget gets closed by the end of the test, so it is highly recommended.
130
168
131
169
:param QWidget widget:
132
170
Widget to keep track of.
133
- '''
171
+ """
134
172
self ._widgets .append (widget )
135
173
136
174
137
175
def waitForWindowShown (self , widget ):
138
- '''
176
+ """
139
177
Waits until the window is shown in the screen. This is mainly useful for asynchronous
140
178
systems like X11, where a window will be mapped to screen some time after being asked to
141
179
show itself on the screen.
142
180
143
181
:param QWidget widget:
144
182
Widget to wait on.
145
- '''
183
+ """
146
184
QtTest .QTest .qWaitForWindowShown (widget )
147
185
148
186
149
187
def stopForInteraction (self ):
150
- '''
188
+ """
151
189
Stops the current test flow, letting the user interact with any visible widget.
152
190
153
191
This is mainly useful so that you can verify the current state of the program while writing
@@ -157,7 +195,7 @@ def stopForInteraction(self):
157
195
of the widgets as they were before this call.
158
196
159
197
.. note:: As a convenience, it is aliased as `stop`.
160
- '''
198
+ """
161
199
widget_visibility = [widget .isVisible () for widget in self ._widgets ]
162
200
163
201
self ._app .exec_ ()
@@ -170,15 +208,12 @@ def stopForInteraction(self):
170
208
stop = stopForInteraction
171
209
172
210
173
- #===================================================================================================
174
- # pytest_configure
175
- #===================================================================================================
176
211
def pytest_configure (config ):
177
- '''
212
+ """
178
213
PyTest plugin API. Called before the start of each test session.
179
214
180
215
:param config.Config config:
181
- '''
216
+ """
182
217
qt_app_instance = QtGui .QApplication ([])
183
218
config .qt_app_instance = qt_app_instance
184
219
@@ -191,56 +226,15 @@ def exit_qapp():
191
226
config ._cleanup .append (exit_qapp )
192
227
193
228
194
- #===================================================================================================
195
- # qtbot
196
- #===================================================================================================
197
229
@pytest .fixture
198
230
def qtbot (request ):
199
- '''
231
+ """
200
232
Fixture used to create a QtBot instance for using during testing.
201
233
202
- Make sure to call addWidget for each top-level widget you create to ensure that they are
203
- properly closed after the test ends.
204
- '''
234
+ Make sure to call addWidget for each top-level widget you create to ensure
235
+ that they are properly closed after the test ends.
236
+ """
205
237
result = QtBot (request .config .qt_app_instance )
206
238
request .addfinalizer (result ._close )
207
239
return result
208
240
209
-
210
-
211
-
212
- #===================================================================================================
213
- # Inject QtTest Functions
214
- #===================================================================================================
215
- def _createQTestProxyMethod (method_name ):
216
-
217
- def result (* args , ** kwargs ):
218
- method = getattr (QtTest .QTest , method_name )
219
- return method (* args , ** kwargs )
220
-
221
- result .__name__ = method_name
222
- return staticmethod (result )
223
-
224
- # inject methods from QTest into QtBot
225
- method_names = set ([
226
- 'keyPress' ,
227
- 'keyClick' ,
228
- 'keyClicks' ,
229
- 'keyEvent' ,
230
- 'keyPress' ,
231
- 'keyRelease' ,
232
- 'keyToAscii' ,
233
-
234
- 'mouseClick' ,
235
- 'mouseDClick' ,
236
- 'mouseEvent' ,
237
- 'mouseMove' ,
238
- 'mousePress' ,
239
- 'mouseRelease' ,
240
-
241
-
242
- ])
243
- for method_name in method_names :
244
- method = _createQTestProxyMethod (method_name )
245
- setattr (QtBot , method_name , method )
246
-
0 commit comments