[PyQt] Testing a standalone dialog widget

Antonio Valentino antonio.valentino at tiscali.it
Mon Dec 29 20:22:43 GMT 2014


Hi Jones, hi Andre,

Il 29/12/2014 20:30, Jones, Bryan ha scritto:
> Andre,
> 
> Have you read through http://johnnado.com/pyqt-qtest-example/? That shows
> testing a dialog box (no threads necessary).
> 
> Bryan

the example linked by Jones terminates with a segmentation fault.
Instantiating QApplication at class level (instead of instantiating
inside the setUp method) seems to fix the issue but I'm not sure if it
is correct.

I'm very interested in this topic so if anyone has some useful pointers
to material about unittesting of PyQt based GUI applications please share.

cheers

> On Sun, Dec 28, 2014 at 7:19 AM, Andre Roberge <andre.roberge at gmail.com>
> wrote:
> 
>>  I'm creating a series of "standalone 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.
>>
>>  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.
>> I would prefer to use 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))
>>
>>
>>  =======
>> Any help would be appreciated.
>>
>>  André Roberge
>>

-- 
Antonio Valentino


More information about the PyQt mailing list