I am writing unit tests for a simple GUI written in PySide 1.2.2. I am working on Windows 7 and with Python 2.7.6.
I want to test this function, which gets activated when a button is clicked.
def setDestination(self):
directory = QtGui.QFileDialog.getExistingDirectory(self, "Select Directory")
self.destLineEdit.setText(directory)
So far I have come up with the following test case:
def test_browse_dest(self):
# Reset the GUI to its defaults
self.clear()
# Click the destination browse button
QTest.mouseClick(self.window.browseButton_2, QtCore.Qt.LeftButton)
# Test paths
destPath = os.path.join(os.getcwd(), TEST_DIR_B, "test")
self.assertEqual(self.window.destLineEdit.text(), destPath)
This test does work, but it is interactive. I have to select the directory and click the Select Folder button. While this is certainly cool and fun to play with, I was wondering if there is a way to automate these actions.
I did try to just hide the file dialog and set the text in the line edit myself. First, I wrote this:
def setDestination(self):
self.fileDialog = QtGui.QFileDialog()
directory = self.fileDialog.getExistingDirectory(self, "Select Directory")
self.destLineEdit.setText(directory)
Then I tried to access the file dialog inside the unit test.
def test_browse_dest(self):
# Reset the GUI to its defaults
self.clear()
# Click the destination browse button
QTest.mouseClick(self.window.browseButton_2, QtCore.Qt.LeftButton)
self.window.fileDialog.hide()
# Test paths
destPath = os.path.join(os.getcwd(), TEST_DIR_B, "test")
self.window.destLineEdit.setText(destPath)
self.assertEqual(self.window.destLineEdit.text(), destPath)
However, that did not work. The file dialog still launched, and I had to interact with it to complete the test.