Skip to content

Commit 3ef965c

Browse files
committed
added first version of the tutorial
1 parent 8210aa8 commit 3ef965c

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

docs/index.rst

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,77 @@ The `pytest-qt`_ package may be installed by running::
3131
This will automatically register ``pytest-qt`` for usage in ``py.test``.
3232

3333

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!
39105

40106
QtBot
41107
=====

pytestqt/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
def test_hello(qtbot):
99
widget = QtGui.QWidget()
1010
qtbot.addWidget(widget)
11-
# test away
11+
12+
# click in the Greet button and make sure it updates the appropriate label
13+
qtbot.mouseClick(window.button_greet, QtCore.Qt.LeftButton)
14+
15+
assert window.greet_label.text() == 'Hello'
1216
1317
1418
.. .. literalinclude:: ../src/pytestqt/_tests/test_basics.py

0 commit comments

Comments
 (0)