On 07/03/2012 09:43, Luca Beltrame wrote:
In data mercoledì 7 marzo 2012 09:37:38, Mads Ipsen ha scritto:
the Python unittest moudle. I have attached a few examples. If you have
time to take a look and comment, it would be great.
I have a question on FooTest: is having a QApplication instance enough in
unittest to ensure emission of signals? Otherwise your problem does not only
apply to using threads, but to any use of unittest with signals and slots.
* Hook up a slot which gets called when the thread finishes
As I said above, are signals emitted in the FooTest case? I ask because I've
been meaning to add unittest to my applications but I was not sure how to
handle the case of slots and signals.
* Start the event loop by calling QtGui.qApp.exec_()
Have attached a new version of the Bar example, that starts an event
loop when the thread is started. In this case, the unit test succeeds.
Bu then again, it this the way to do it?
I think I've tried this in the past and it didn't quite work for me.
_______________________________________________
PyQt mailing list PyQt@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.ip...@gmail.com |
+----------------------+------------------------------+
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._finished = False
self._thread = Thread()
self.connect(self._thread, QtCore.SIGNAL('finished()'), self.finished)
self._thread.start()
def finished(self):
self._finished = True
import unittest
import sys
import gc
from PyQt4 import QtCore, 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 Runner:
def __init__(self, thread):
self._thread = thread
self._thread.connect(self._thread, QtCore.SIGNAL('finished()'), self.finished)
if self._thread.isRunning():
QtGui.qApp.exec_()
def finished(self):
QtGui.qApp.processEvents()
QtGui.qApp.exit(0)
class BarTest(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 """
runner = Runner(self._widget._thread)
self.assertTrue(isinstance(self._widget, Widget))
self.assertTrue(isinstance(self._widget, QtGui.QWidget))
def testThread(self):
""" Test that the thread finished """
runner = Runner(self._widget._thread)
self.assertTrue(self._widget._finished)
if __name__ == '__main__':
unittest.main()
_______________________________________________
PyQt mailing list PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt