[PyQt] Unit testing PyQt applications

Mads Ipsen madsipsen at gmail.com
Wed Mar 7 08:37:38 GMT 2012


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

But this easily becomes messy, when you have a large and complex code 
base, and I was wondering if there's some more generic approach.

I should add, that in my case I have a large nested unittest suite, 
which collects all the individual unittest files. In that case, the 
instance of a QApplication is only started once, by an __init__.py file, 
which gets loaded once, when the entire suite it run.

All that said, if you have any comments, pointers to good approaches, or 
like to share your ideas, I'd be happy to hear them.

Best regards,

Mads



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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20120307/b3f82021/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Foo.py
Type: text/x-python
Size: 135 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20120307/b3f82021/attachment.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: FooTest.py
Type: text/x-python
Size: 608 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20120307/b3f82021/attachment-0001.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Bar.py
Type: text/x-python
Size: 579 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20120307/b3f82021/attachment-0002.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: BarTest.py
Type: text/x-python
Size: 729 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20120307/b3f82021/attachment-0003.py>


More information about the PyQt mailing list