[PyQt] unit testing pyqt app components

Florian Bruhin me at the-compiler.org
Tue Sep 1 05:19:59 BST 2015


* oliver <oliver.schoenborn at gmail.com> [2015-08-31 23:38:31 -0400]:
> 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.

I'd recommend switching to pytest[1] with its excellent pytest-qt[2]
plugin which solves those problems for you ;)

It can even run nose tests, so you can gradually switch over.

It also has a way[3] to wait until a signal is emitted (with a
timeout) where it spins a QEventLoop.

(disclaimer: I'm a contributor to both pytest and the plugin)

[1] https://github.com/pytest-dev/pytest
[2] https://github.com/pytest-dev/pytest-qt
[3] https://pytest-qt.readthedocs.org/en/1.5.1/signals.html

> 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?

I don't think you should reuse a QApplication after you (I guess)
called QApplication::quit.

pytest-qt runs QApplication::processEvents before and after each test
- it's not ideal, but it's the best we could come up with so far.

Another solution might be to run a dedicated QEventLoop every time,
like the waitSignal thing in [3].

Florian

-- 
http://www.the-compiler.org | me at the-compiler.org (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072 | http://the-compiler.org/pubkey.asc
         I love long mails! | http://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150901/20cfc9ab/attachment.sig>


More information about the PyQt mailing list