[PyQt] Unit testing PyQt applications

Mads Ipsen madsipsen at gmail.com
Wed Mar 7 14:42:34 GMT 2012

OK, I agree, the snippets I provided were not nice, and I apologize for 

But suppose that the constructor in your class starts a thread that 
generates say an icon for a QStandardItem. Then you want to make sure 
that the thread has finished before you test that the icon has been created.

* What would be the proper way of unit testing this?
* How should starting/stopping the event loop be handled?

Best regards,


On 07/03/2012 10:51, Andreas Pakulat wrote:
> On 07.03.12 09:37:38, Mads Ipsen wrote:
>> Hi,
>> I have a general question about how to unit test PyQt applications
>> using the Python unittest moudle. I have attached a few examples. If
>> you have time to take a look and comment, it would be great.
>> The first module, Foo.py, defines a simple widget called 'Widget'
>> which is tested in FooTest.py. FooTest.py defines an instance of a
>> QApplication, which is needed in order to run the test. In this case
>> the test runs fine.
>> The second module, Bar.py, defines a more complex widget, also
>> called Widget, which starts a thread in its constructor. In real
>> life, this could be a used for generating an icon in the background.
>> This is tested in BarTest.py. In this case, the test fails, because
>> the unittest exits while the thread is still running.
>> There are ways to circumvent this, e.g:
>> * Hook up a slot which gets called when the thread finishes
>> * Start the event loop by calling QtGui.qApp.exec_()
>> * Kill the event loop when the slot is called
> Your example code is simply buggy IMHO. If an object starts a thread it
> should stop and clean that thread up once the object is destroyed.
> This can be done through the destroyed signal. Once your widget cleans
> up the thread you shouldn't have the problem anymore and you don't need
> the finished-state either.
> Andreas
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt

| Mads Ipsen                                          |
| Gåsebæksvej 7, 4. tv |                              |
| DK-2500 Valby        | phone:          +45-29716388 |
| Denmark              | email:  mads.ipsen at gmail.com |

More information about the PyQt mailing list