[PyQt] Unit testing PyQt applications

Andreas Pakulat apaku at gmx.de
Wed Mar 7 09:51:59 GMT 2012


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



More information about the PyQt mailing list