Functional Test for QMessageBox... why does not work?
Asked Answered
S

1

2

I would develop some functional tests for a pyqt application that uses PyQt (or PySide) as GUI library. The tests use Unittest and Qttest library, as reported in many resources, for example this stackoverflow question: Unit and functional testing a PySide-based application? For the main window all works fine, and the code simulate perfectly Keyboard Types and Mouse Clicks and Movements, but the "devil is in the details"... and this method does not work for a QMessageBox.

In the class of the Main Window, for manage a IOError on opening a file, I initialize a QMessageBox:

self.IOErrMsgBox = QtGui.QMessageBox()
self.IOErrMsgBox.setText("<b>Error</b>")
self.IOErrMsgBox.setInformativeText("""
                                    <p>There was an error opening
                                    the project file:
                                    %s.</p>"""%(path,))
self.IOErrMsgBox.setStandardButtons(QtGui.QMessageBox.Ok)
self.IOErrMsgBox.setDefaultButton(QtGui.QMessageBox.Ok)
self.IOErrMsgBox.exec_()

To test how it works, in functional test I have:

def test__open_project(self):
    self.MainWin._project_open(wrong_path, flag='c') 
    # the function that handles the exception 
    # and initializes the QMessageBox.
    IOErrMsgBox = self.MainWin.IOErrMsgBox
    # Reference to the initialized QMessageBox.
    self.assertIsInstance(IOErrMsgBox, QMessageBox)
    okWidget = self.MainWin.IOErrMsgBox.button(IOErrMsgBox.Ok)
    QTest.mouseClick(okWidget, Qt.LeftButton)

or, in altenative:

def test__open_project(self):
     #... some code, exactly like previous example except for last row...
     QTest.keyClick(okWidget, 'o', Qt.AltModifier)

but No one works... and the Ok button is not clicked and I can do it with my mouse pointer :(

Any suggestions?

Suzetta answered 24/6, 2014 at 9:16 Comment(0)
H
4

The question is in general about how to test modal dialogs.

Any modal dialog including QMessageBox will not return from exec_() until it is closed, so the test code in your second code box probably never gets executed.

You could just show() it (making it non-modal) and then follow your code but don't forget to close and delete the dialog afterwards.

Or you use a Timer and schedule a click on the OK button (similar to Test modal dialog with Qt Test). Here is an example:

from PySide import QtGui, QtCore

app = QtGui.QApplication([])

box = QtGui.QMessageBox()
box.setStandardButtons(QtGui.QMessageBox.Ok)
button = box.button(QtGui.QMessageBox.Ok)
QtCore.QTimer.singleShot(0, button.clicked)
box.exec_()
Horehound answered 30/6, 2014 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.