Qt event loop and unit testing?

后端 未结 3 737
Happy的楠姐
Happy的楠姐 2020-12-16 11:05

I\'we started experimenting with unit testing in Qt and would like to hear comments on a scenario that involves unit testing signals and slots.

Here is an example:

3条回答
  •  醉梦人生
    2020-12-16 11:57

    I had a similar issue with Qt::QueuedConnection (event is queued automatically if the sender and the receiver belongs to different threads). Without a proper event loop in that situation, the internal state of objects depending on event processing will not be updated. To start an event loop when running QTest, change the macro QTEST_APPLESS_MAIN at the bottom of the file to QTEST_MAIN. Then, calling qApp->processEvents() will actually process events, or you can start another event loop with QEventLoop.

       QSignalSpy spy(&foo, SIGNAL(ready()));
       connect(&foo, SIGNAL(ready()), &bar, SLOT(work()), Qt::QueuedConnection);
       foo.emitReady();
       QCOMPARE(spy.count(), 1);        // QSignalSpy uses Qt::DirectConnection
       QCOMPARE(bar.received, false);   // bar did not receive the signal, but that is normal: there is no active event loop
       qApp->processEvents();           // Manually trigger event processing ...
       QCOMPARE(bar.received, true);    // bar receives the signal only if QTEST_MAIN() is used
    

提交回复
热议问题