[PyQt] unit testing pyqt app components

Florian Bruhin me at the-compiler.org
Thu Sep 3 15:38:36 BST 2015


* oliver <oliver.schoenborn at gmail.com> [2015-09-03 09:41:52 -0400]:
> Thanks I'll check it out, if anything I can use some of the techniques. But
> if I were to switch some tests over to pytest+qt plugin, basically the
> first test would create the QApplication, and once the exec is entered in
> first test, it never returns until the whole test suite is done? How does
> the test driver ever regain control to start the next test (which will
> presumably just create a widget and not call exec())?

The QApplication is created by the plugin ahead of the first test -
and exec() is never called on it, only QApplication::processEvents
(which processes all queued events and then returns).

(That is, unless you use the qtbot.waitSignal function - then
QEventLoop.exec_() is called until the timeout has passed or the
signal was emitted)

> With nose, we use a class that allows us to schedule "actions" (like
> clicking a button, verifying the state of some object, etc). A test
> basically schedules a bunch of actions (callbacks defined in the test) then
> enters a custom exec() that adds a "quit" action to exit the event loop
> once all actions are done, and calls the app exec(). Then control goes back
> to nose and the process repeats.

Interesting approach - is the code for this available somewhere?

With pytest-qt, you'd do something like:

    def test_button(qtbot):
        window = YourWindow()
        with qtbot.waitSignal(window.action_done):
            qtbot.click(window.start_action_button)

> I don't think QApplication ever gets destroyed, but its exec() is
> therefore called multiple times. Isn't this how QTestLib.exec() is
> meant to be used?

I can't find a QTestLib.exec() (and neither QTest) in the Qt
documentation - what do you mean exactly?

As for QApplication.exec, at least the documentation doesn't mention
something special for QApplication.exit/quit - and looking at Qt's own
(C++) tests, that seems to be done there as well.

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/20150903/4f1dc6bb/attachment.sig>


More information about the PyQt mailing list