@@ -31,11 +31,77 @@ The `pytest-qt`_ package may be installed by running::
31
31
This will automatically register ``pytest-qt `` for usage in ``py.test ``.
32
32
33
33
34
- Tutorial
35
- ========
36
-
37
- Soon.
38
-
34
+ Quick Tutorial
35
+ ==============
36
+
37
+ ``pytest-qt `` registers a new fixture _ named ``qtbot ``. This fixture acts as *bot * in the sense
38
+ that it can send keyboard and mouse events to any widgets being tested. This way, the programmer
39
+ can simulate user interaction while checking if GUI controls are behaving in the expected manner.
40
+
41
+ .. _fixture : http://pytest.org/latest/fixture.html
42
+
43
+ To illustrate that, consider a widget constructed to allow the user to find files in a given
44
+ directory inside an application.
45
+
46
+ .. image :: _static/find_files_dialog.png
47
+ :align: center
48
+
49
+ It is a very simple dialog, where the user enters a standard file mask, optionally enters file text
50
+ to search for and a button to browse for the desired directory. Its source code is available here _,
51
+
52
+ .. _here : https://github.com/nicoddemus/PySide-Examples/blob/master/examples/dialogs/findfiles.py
53
+
54
+ To test this widget's basic functionality, create a test function::
55
+
56
+ def test_basic_search(qtbot, tmpdir):
57
+ '''
58
+ test to ensure basic find files functionality is working.
59
+ '''
60
+ tmpdir.join('video1.avi').ensure()
61
+ tmpdir.join('video1.srt').ensure()
62
+
63
+ tmpdir.join('video2.avi').ensure()
64
+ tmpdir.join('video2.srt').ensure()
65
+
66
+ Here the first parameter indicates that we will be using a ``qtbot `` fixture to control our widget.
67
+ The other parameter is py.test standard's tmpdir _ that we use to create some files that will be
68
+ used during our test.
69
+
70
+ .. _tmpdir : http://pytest.org/latest/tmpdir.html
71
+
72
+ Now we create the widget to test and register it::
73
+
74
+ window = Window()
75
+ window.show()
76
+ qtbot.addWidget(window)
77
+
78
+ .. tip :: Registering widgets is not required, but recommended because it will ensure those widgets get
79
+ properly closed after each test is done.
80
+
81
+ Now we use ``qtbot `` methods to simulate user interaction with the dialog::
82
+
83
+ window.fileComboBox.clear()
84
+ qtbot.keyClicks(window.fileComboBox, '*.avi')
85
+
86
+ window.directoryComboBox.clear()
87
+ qtbot.keyClicks(window.directoryComboBox, str(tmpdir))
88
+
89
+ The method ``keyClicks `` is used to enter text in the editable combo box, selecting the desired mask
90
+ and directory.
91
+
92
+ We then simulate a user clicking the button with the ``mouseClick `` method::
93
+
94
+ qtbot.mouseClick(window.findButton, QtCore.Qt.LeftButton)
95
+
96
+ Once this is done, we inspect the results widget to ensure that it contains the expected files we
97
+ created earlier::
98
+
99
+ assert window.filesTable.rowCount() == 2
100
+ assert window.filesTable.item(0, 0).text() == 'video1.avi'
101
+ assert window.filesTable.item(1, 0).text() == 'video2.avi'
102
+
103
+
104
+ And that's it for this quick tutorial!
39
105
40
106
QtBot
41
107
=====
0 commit comments