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.ip...@gmail.com |
+----------------------+------------------------------+


from PyQt4 import QtGui

class Widget(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)


        
        
import unittest
import sys
import gc

from PyQt4 import QtGui

# Must have an event handler running
QT_APP = QtGui.QApplication(sys.argv)

# Import the module that should be tested
from Foo import Widget

class ModelTest(unittest.TestCase):

    def setUp(self):
        self._widget = Widget()

    def tearDown(self):
        del self._widget
        gc.collect()

    def testWidget(self):
        """ Test that the object is OK """
        self.assertTrue(isinstance(self._widget, Widget))
        self.assertTrue(isinstance(self._widget, QtGui.QWidget))

if __name__ == '__main__':
    unittest.main()

import time

from PyQt4 import QtCore, QtGui

class Thread(QtCore.QThread):
    def __init__(self):
        QtCore.QThread.__init__(self)

    def run(self):
        for i in range(10):
            print i
            time.sleep(0.1)

class Widget(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self._thread = Thread()
        self._thread.start()
        self._finished = False

        self.connect(self._thread, QtCore.SIGNAL('finished'), self.finished)

    def finished(self):
        self._finished = True
        


        
        
import unittest
import sys
import gc

from PyQt4 import QtGui

# Must have an event handler running
QT_APP = QtGui.QApplication(sys.argv)

# Import the module that should be tested
from Bar import Widget

class ModelTest(unittest.TestCase):

    def setUp(self):
        self._widget = Widget()

    def tearDown(self):
        del self._widget
        gc.collect()

    def testWidget(self):
        """ Test that the object is OK """
        self.assertTrue(isinstance(self._widget, Widget))
        self.assertTrue(isinstance(self._widget, QtGui.QWidget))

    def testThread(self):
        """ Test that the thread finished """
        self.assertTrue(self._widget._finished)

if __name__ == '__main__':
    unittest.main()

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to