22from pytestqt .qt_compat import QtGui
33from pytestqt .qt_compat import QtTest
44
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
846class QtBot (object ):
9- '''
47+ """
1048 Instances of this class are responsible for sending events to `Qt` objects (usually widgets),
1149 simulating user input.
1250
@@ -103,51 +141,51 @@ class QtBot(object):
103141
104142 .. _QTest API: http://doc.qt.digia.com/4.7/qtest.html
105143
106- '''
144+ """
107145
108146 def __init__ (self , app ):
109- '''
147+ """
110148 :param QApplication app:
111149 The current QApplication instance.
112- '''
150+ """
113151 self ._app = app
114152 self ._widgets = []
115153
116154
117155 def _close (self ):
118- '''
156+ """
119157 Clear up method. Called at the end of each test that uses a ``qtbot`` fixture.
120- '''
158+ """
121159 for w in self ._widgets :
122160 w .close ()
123161 self ._widgets [:] = []
124162
125163
126164 def addWidget (self , widget ):
127- '''
165+ """
128166 Adds a widget to be tracked by this bot. This is not required, but will ensure that the
129167 widget gets closed by the end of the test, so it is highly recommended.
130168
131169 :param QWidget widget:
132170 Widget to keep track of.
133- '''
171+ """
134172 self ._widgets .append (widget )
135173
136174
137175 def waitForWindowShown (self , widget ):
138- '''
176+ """
139177 Waits until the window is shown in the screen. This is mainly useful for asynchronous
140178 systems like X11, where a window will be mapped to screen some time after being asked to
141179 show itself on the screen.
142180
143181 :param QWidget widget:
144182 Widget to wait on.
145- '''
183+ """
146184 QtTest .QTest .qWaitForWindowShown (widget )
147185
148186
149187 def stopForInteraction (self ):
150- '''
188+ """
151189 Stops the current test flow, letting the user interact with any visible widget.
152190
153191 This is mainly useful so that you can verify the current state of the program while writing
@@ -157,7 +195,7 @@ def stopForInteraction(self):
157195 of the widgets as they were before this call.
158196
159197 .. note:: As a convenience, it is aliased as `stop`.
160- '''
198+ """
161199 widget_visibility = [widget .isVisible () for widget in self ._widgets ]
162200
163201 self ._app .exec_ ()
@@ -170,15 +208,12 @@ def stopForInteraction(self):
170208 stop = stopForInteraction
171209
172210
173- #===================================================================================================
174- # pytest_configure
175- #===================================================================================================
176211def pytest_configure (config ):
177- '''
212+ """
178213 PyTest plugin API. Called before the start of each test session.
179214
180215 :param config.Config config:
181- '''
216+ """
182217 qt_app_instance = QtGui .QApplication ([])
183218 config .qt_app_instance = qt_app_instance
184219
@@ -191,56 +226,15 @@ def exit_qapp():
191226 config ._cleanup .append (exit_qapp )
192227
193228
194- #===================================================================================================
195- # qtbot
196- #===================================================================================================
197229@pytest .fixture
198230def qtbot (request ):
199- '''
231+ """
200232 Fixture used to create a QtBot instance for using during testing.
201233
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+ """
205237 result = QtBot (request .config .qt_app_instance )
206238 request .addfinalizer (result ._close )
207239 return result
208240
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