@@ -18,25 +18,29 @@ to search for and a button to browse for the desired directory. Its source code
18
18
19
19
.. _here : https://github.com/nicoddemus/PySide-Examples/blob/master/examples/dialogs/findfiles.py
20
20
21
- To test this widget's basic functionality, create a test function::
21
+ To test this widget's basic functionality, create a test function:
22
22
23
- def test_basic_search(qtbot, tmpdir):
24
- '''
23
+ .. code-block :: python
24
+
25
+ def test_basic_search (qtbot , tmp_path ):
26
+ """
25
27
test to ensure basic find files functionality is working.
26
- '''
27
- tmpdir.join(' video1.avi').ensure ()
28
- tmpdir.join(' video1.srt').ensure ()
28
+ """
29
+ tmp_path.joinpath( " video1.avi" ).touch ()
30
+ tmp_path.joinpath( " video1.srt" ).touch ()
29
31
30
- tmpdir.join(' video2.avi').ensure ()
31
- tmpdir.join(' video2.srt').ensure ()
32
+ tmp_path.joinpath( " video2.avi" ).touch ()
33
+ tmp_path.joinpath( " video2.srt" ).touch ()
32
34
33
35
Here the first parameter indicates that we will be using a ``qtbot `` fixture to control our widget.
34
36
The other parameter is pytest's standard tmpdir _ that we use to create some files that will be
35
37
used during our test.
36
38
37
39
.. _tmpdir : http://pytest.org/latest/tmpdir.html
38
40
39
- Now we create the widget to test and register it::
41
+ Now we create the widget to test and register it:
42
+
43
+ .. code-block :: python
40
44
41
45
window = Window()
42
46
window.show()
@@ -45,24 +49,50 @@ Now we create the widget to test and register it::
45
49
.. tip :: Registering widgets is not required, but recommended because it will ensure those widgets get
46
50
properly closed after each test is done.
47
51
48
- Now we use ``qtbot `` methods to simulate user interaction with the dialog::
52
+ Now we can interact with the widgets directly:
53
+
54
+ .. code-block :: python
49
55
50
56
window.fileComboBox.clear()
51
- qtbot.keyClicks( window.fileComboBox, ' *.avi' )
57
+ window.fileComboBox.setCurrentText( " *.avi" )
52
58
53
59
window.directoryComboBox.clear()
54
- qtbot.keyClicks(window.directoryComboBox, str(tmpdir))
60
+ window.directoryComboBox.setCurrentText(str (tmp_path))
61
+
62
+
63
+ We use the ``QComboBox.setCurrentText `` method to change the current item selected in the combo box.
64
+
65
+
66
+ .. _note-about-qtbot-methods :
55
67
56
- The method ``keyClicks `` is used to enter text in the editable combo box, selecting the desired mask
57
- and directory.
68
+ .. note ::
58
69
59
- We then simulate a user clicking the button with the ``mouseClick `` method::
70
+ In general, prefer to use a widget's own methods to interact with it: ``QComboBox.setCurrentIndex ``, ``QLineEdit.setText ``,
71
+ etc. Those methods will emit the appropriate signal, so the test will work just the same as if the user themselves
72
+ have interacted with the controls.
60
73
61
- qtbot.mouseClick(window.findButton, QtCore.Qt.LeftButton)
74
+ Note that ``qtbot `` provides a number of methods to simulate actual interaction, for example ``keyClicks ``, ``mouseClick ``,
75
+ etc. Those methods should be used only in specialized situations, for example if you are creating a custom drawing widget
76
+ and want to simulate actual clicks.
77
+
78
+ For normal interactions, always prefer widget methods (``setCurrentIndex ``, ``setText ``, etc) -- ``qtbot ``'s methods
79
+ (``keyClicks ``, ``mouseClick ``, etc) will trigger an actual event, which will then need to be processed in the next
80
+ pass of the event loop, making the test unreliable and flaky. Also some operations are hard to simulate using
81
+ raw clicks, for example selecting an item on a ``QComboBox ``, which will need two ``mouseClick ``
82
+ calls to simulate properly, while figuring out where to click.
83
+
84
+
85
+ We then simulate a user clicking the button:
86
+
87
+ .. code-block :: python
88
+
89
+ window.findButton.click()
62
90
63
91
Once this is done, we inspect the results widget to ensure that it contains the expected files we
64
- created earlier::
92
+ created earlier:
93
+
94
+ .. code-block :: python
65
95
66
96
assert window.filesTable.rowCount() == 2
67
- assert window.filesTable.item(0, 0).text() == ' video1.avi'
68
- assert window.filesTable.item(1, 0).text() == ' video2.avi'
97
+ assert window.filesTable.item(0 , 0 ).text() == " video1.avi"
98
+ assert window.filesTable.item(1 , 0 ).text() == " video2.avi"
0 commit comments