[PyQt] unit testing pyqt app components

oliver oliver.schoenborn at gmail.com
Tue Sep 1 04:38:31 BST 2015


We have a large pyqt-based application, PyQt works awesome. We use python
nose for unit tests of business logic, and we have a set of unit tests for
the GUI components.

For these, each test class's setup() instantiates QApplication if
QApplication.instance() is None, starts a second QThread (needed by the
application), intantiates the GUI component to be tested in main thread,
connects GUI component to objects that get modified by the second thread,
queues some functions that will get fired some seconds in the future (via
QTimer) to simulate user actions, then enters the event loop via
QApplication's exec(). The functions call methods on the GUI component and
assert its resulting state is as expected. This works well, Qt is amazing
for its ability to generate events and pretend user actions.

However, sometimes it appears that resources are not properly cleaned up
between tests: a test will crash, or will not start unless we have put a
"del some_qwidget_instance" in the teardown. Is it safe to re-use the same
QApplication instance after its exec() has returned? I.e. can we do this:

app = QApplication([])
my_gui_obj = MyGuiObj()  # derives from QWidget
# other stuff
app.exec()
my_gui_obj2 = MyGuiObj()  # derives from QWidget
# other stuff
app.exec()
my_gui_obj3 = MyGuiObj()  # derives from QWidget
# other stuff
app.exec()

Is there anything we should do to QApplication instance between tests
(clear some caches, reset things like styles etc) so that each tests starts
with a "fresh" QApplication? What is the recommended way to test multiple
PyQt components independently of each other?

-- 
Oliver
Author of these Open Source: PyPubSub <http://pubsub.sf.net>, Lua-iCxx
<http://lua-icxx.sf.net>, iof <http://iof.sf.net>
Regular contributor on StackOverflow
<http://stackoverflow.com/users/869951/schollii>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150831/62962840/attachment.html>


More information about the PyQt mailing list