lundi 29 décembre 2014

Unit testing a PyQt standalone "blocking" dialog

I'm creating a series of "standalone PyQt widgets" that can be used in procedural programs; the original idea is from the easygui project. To give a concrete idea of their use, instead of writing:



>>> name = input("What is your name? )


I could write



>>> name = get_string("What is your name? )


and a dialog would pop up, inviting the user to enter the response. Like Python's input, get_string is blocking the execution of the program, waiting for input.


I would like to set up some automatic testing of these widgets. I tried with a third-party module (pyautogui) which interacts with GUI programs, but the result is not totally reliable and probably not suitable for using a service like https://travis-ci.org/. I would prefer to use PyQt's QTest but do not know how to connect with the dialog: I suspect I may have to use threading (as I had to do with the pyautogui solution) as the dialog is effectively blocking the execution of the program.


Here is a simple implementation of get_string() mentioned above:



from PyQt4 import QtGui

def get_string(prompt="What is your name? ", title="Title",
default_response="PyQt4", app=None):
"""GUI equivalent of input()."""

if app is None:
app = QtGui.QApplication([])
app.dialog = QtGui.QInputDialog()
text, ok = app.dialog.getText(None, title, prompt,
QtGui.QLineEdit.Normal,
default_response)
app.quit()
if ok:
return text

if __name__ == '__main__':
print(get_string()) # normal blocking mode
app2 = QtGui.QApplication([])
# perhaps start a delayed thread here, using QTest
print(get_string(app=app2))


I have found one example of unit-testing a PyQt application (http://johnnado.com/pyqt-qtest-example/) but it did not help me find a solution in terms of connecting to a dialog.


Any help would be appreciated.


Aucun commentaire:

Enregistrer un commentaire