Skip to content

Commit 7b0a65a

Browse files
committed
code format and using class decorator to inject QTest methods
1 parent cd1c3e4 commit 7b0a65a

File tree

2 files changed

+80
-104
lines changed

2 files changed

+80
-104
lines changed

pytestqt/_tests/test_basics.py

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,11 @@
22
import pytest
33

44

5-
#===================================================================================================
6-
# fixtures
7-
#===================================================================================================
8-
@pytest.fixture
9-
def event_recorder(qtbot):
10-
widget = EventRecorder()
11-
qtbot.addWidget(widget)
12-
return widget
13-
14-
15-
#===================================================================================================
16-
# testBasics
17-
#===================================================================================================
18-
def testBasics(qtbot):
19-
'''
5+
def test_basics(qtbot):
6+
"""
207
Basic test that works more like a sanity check to ensure we are setting up a QApplication
218
properly and are able to display a simple event_recorder.
22-
'''
9+
"""
2310
assert type(QtGui.qApp) is QtGui.QApplication
2411
widget = QtGui.QWidget()
2512
qtbot.addWidget(widget)
@@ -30,13 +17,10 @@ def testBasics(qtbot):
3017
assert widget.windowTitle() == 'W1'
3118

3219

33-
#===================================================================================================
34-
# testKeyEvents
35-
#===================================================================================================
36-
def testKeyEvents(qtbot, event_recorder):
37-
'''
20+
def test_key_events(qtbot, event_recorder):
21+
"""
3822
Basic key events test.
39-
'''
23+
"""
4024
def extract(key_event):
4125
return (
4226
key_event.type(),
@@ -53,20 +37,17 @@ def extract(key_event):
5337
assert event_recorder.event_data == (QEvent.KeyRelease, int(Qt.Key_A), 'a')
5438

5539

56-
#===================================================================================================
57-
# testMouseEvents
58-
#===================================================================================================
59-
def testMouseEvents(qtbot, event_recorder):
60-
'''
40+
def test_mouse_events(qtbot, event_recorder):
41+
"""
6142
Basic mouse events test.
62-
'''
43+
"""
6344
def extract(mouse_event):
6445
return (
6546
mouse_event.type(),
6647
mouse_event.button(),
6748
mouse_event.modifiers(),
6849
)
69-
50+
7051
event_recorder.registerEvent(QtGui.QMouseEvent, extract)
7152

7253
qtbot.mousePress(event_recorder, Qt.LeftButton)
@@ -76,17 +57,14 @@ def extract(mouse_event):
7657
assert event_recorder.event_data == (QEvent.MouseButtonPress, Qt.RightButton, Qt.AltModifier)
7758

7859

79-
#===================================================================================================
80-
# EventRecorder
81-
#===================================================================================================
8260
class EventRecorder(QtGui.QWidget):
83-
'''
61+
"""
8462
Widget that records some kind of events sent to it.
8563
8664
When this event_recorder receives a registered event (by calling `registerEvent`), it will call
8765
the associated *extract* function and hold the return value from the function in the
8866
`event_data` member.
89-
'''
67+
"""
9068

9169
def __init__(self):
9270
QtGui.QWidget.__init__(self)
@@ -105,10 +83,14 @@ def event(self, ev):
10583
return True
10684

10785
return False
108-
109-
110-
#===================================================================================================
111-
# main
112-
#===================================================================================================
86+
87+
88+
@pytest.fixture
89+
def event_recorder(qtbot):
90+
widget = EventRecorder()
91+
qtbot.addWidget(widget)
92+
return widget
93+
94+
11395
if __name__ == '__main__':
11496
pytest.main(args=['-s'])

pytestqt/plugin.py

Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,49 @@
22
from pytestqt.qt_compat import QtGui
33
from 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
846
class 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-
#===================================================================================================
176211
def 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
198230
def 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

Comments
 (0)