[PyQt] unit testing pyqt app components

oliver oliver.schoenborn at gmail.com
Mon Sep 7 19:10:33 BST 2015


On Thu, Sep 3, 2015 at 10:38 AM, Florian Bruhin <me at the-compiler.org> wrote:

> * 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).
>

So sendPostedEvents() is not needed (except after quitting the loop,
judging by qcoreapplication::exec() source)?

> 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?
>
> Not right now, unfortunately. I'm hoping client agrees to release some of
this stuff soon.


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

I meant QTest.qExec() (see http://doc.qt.io/qt-5/qtest.html#qExec and
https://github.com/qtproject/qtbase/blob/5f5db1c38b288441d02a68f1a095850b42585e04/src/testlib/qtestcase.cpp
)


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


Looks like the exec() creates a QEventLoop instance, executes it, and on
return from that instance's exec() and before returning to caller, does
some cleanup (the aboutToQuit signal, a final sendPostedEvents). Is
https://github.com/qtproject/qtbase the best online place to look at Qt
source?

Oliver
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150907/27b7a92f/attachment.html>


More information about the PyQt mailing list