Qt event loop and unit testing?

后端 未结 3 738
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:49

    I realize this is an old thread but as I hit it and as others will, there is no answer and the answer by peter and other comments still miss the point of using QSignalSpy.

    To answer you original question about "where the QCoreApplication exec function is needed", basically the answer is, it isn't. QTest and QSignalSpy already has that built in.

    What you really need to do in your test case is "run" the existing event loop.

    Assuming you are using Qt 5: http://doc.qt.io/qt-5/qsignalspy.html#wait

    So to modify your example to use the wait function:

    void CommunicationProtocolTest::testConnectToCammera()
    {
        QSignalSpy spy(communicationProtocol->m_socket, SIGNAL(connected()));
        communicationProtocol->connectToCamera();
    
        // wait returns true if 1 or more signals was emitted
        QCOMPARE(spy.wait(250), true);
    
        // You can be pedantic here and double check if you want
        QCOMPARE(spy.count(), 1);
    }
    

    That should give you the desired behaviour without having to create another event loop.

    0 讨论(0)
  • 2020-12-16 11:52

    Good question. Main issues I've hit are (1) needing to let app do app.exec() yet still close-at-end to not block automated builds and (2) needing to ensure pending events get processed before relying on the result of signal/slot calls.

    For (1), you could try commenting out the app.exec() in main(). BUT then if someone has FooWidget.exec() in their class that you're testing, it's going to block/hang. Something like this is handy to force qApp to exit:

    int main(int argc, char *argv[]) {
        QApplication a( argc, argv );   
    
        //prevent hanging if QMenu.exec() got called
        smersh().KillAppAfterTimeout(300);
    
        ::testing::InitGoogleTest(&argc, argv);
        int iReturn = RUN_ALL_TESTS(); 
        qDebug()<<"rcode:"<<iReturn;
    
        smersh().KillAppAfterTimeout(1);
        return a.exec();
       }
    
    struct smersh {
      bool KillAppAfterTimeout(int secs=10) const;
    };
    
    bool smersh::KillAppAfterTimeout(int secs) const {
      QScopedPointer<QTimer> timer(new QTimer);
      timer->setSingleShot(true);
      bool ok = timer->connect(timer.data(),SIGNAL(timeout()),qApp,SLOT(quit()),Qt::QueuedConnection);
      timer->start(secs * 1000); // N seconds timeout
      timer.take()->setParent(qApp);
      return ok;
    }
    

    For (2), basically you have to coerce QApplication into finishing up the queued events if you're trying to verify things like QEvents from Mouse + Keyboard have expected outcome. This FlushEvents<>() method is helpful:

    template <class T=void> struct FlushEvents {     
     FlushEvents() {
     int n = 0;
     while(++n<20 &&  qApp->hasPendingEvents() ) {
       QApplication::sendPostedEvents();
       QApplication::processEvents(QEventLoop::AllEvents);
       YourThread::microsec_wait(100);
     }
     YourThread::microsec_wait(1*1000);
    } };
    

    Usage example below. "dialog" is instance of MyDialog. "baz" is instance of Baz. "dialog" has a member of type Bar. When a Bar selects a Baz, it emits a signal; "dialog" is connected to the signal and we need to make sure the associated slot has gotten the message.

    void Bar::select(Baz*  baz) {
      if( baz->isValid() ) {
         m_selected << baz;
         emit SelectedBaz();//<- dialog has slot for this
    }  }    
    
    TEST(Dialog,BarBaz) {  /*<code>*/
    dialog->setGeometry(1,320,400,300); 
    dialog->repaint();
    FlushEvents<>(); // see it on screen (for debugging)
    
    //set state of dialog that has a stacked widget
    dialog->setCurrentPage(i);
    qDebug()<<"on page: "
            <<i;      // (we don't see it yet)
    FlushEvents<>();  // Now dialog is drawn on page i 
    
    dialog->GetBar()->select(baz); 
    FlushEvents<>(); // *** without this, the next test
                     //           can fail sporadically.
    
    EXPECT_TRUE( dialog->getSelected_Baz_instances()
                                     .contains(baz) );
    /*<code>*/
    }
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题