Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to properly start Qt with Qt Eventloop for GTest



  • Hey,
    I'd like to create some GTests on QT Objects. However, if need the Signals/Slots to work.
    Therefore I run QApplication in main() and from main() I spawn a QThread() running Gtest.
    I'm running Qt 5.12.8

    /**
     * @brief Executes GTest. After Executing GTest it will stop the QApllication
    */
    static void ExecuteGTest();
    
    int main(int argc, char* argv[])
    {
    
    
      ::testing::InitGoogleTest(&argc, argv);
      QApplication app(argc, argv);
    
      std::unique_ptr<QThread> pGTest(QThread::create(ExecuteGTest));
      pGTest->start();
    
      const int iGTestRes = app.exec();
      bool bIsRunning = pGTest->isRunning();
      if (bIsRunning)
      {
        //GTest thread is still running =>should not been happening....requesting exit
        //Request exit with error code
        pGTest->exit(-1);
        pGTest->wait();
      }
      return iGTestRes;
    }
    
    static void ExecuteGTest()
    {
      {
        //perform checks to validate that Qt Eventloop is actually running
        const QThread* pCurrent = QThread::currentThread();
        ASSERT_TRUE(nullptr != pCurrent) << "This is no QThread";
        const auto* pEventLoop = pCurrent->eventDispatcher();
        ASSERT_TRUE(nullptr != pEventLoop) << "Eventloop is not running";
      }
      const int iRes = RUN_ALL_TESTS();
      //terminate the Qt Eventloop and return GTest Result
      QApplication::exit(iRes);
    }
    

    Now my Testcase is pretty simple

    static bool mg_bTimerExpired = false;
    
    static void OnTimerTick();
    
    static void OnTimerTick()
    {
      mg_bTimerExpired = false;
    }
    
    
    
    TEST(RunCameraUpdate, FirstTest)
    {
      QTimer::singleShot(2000, nullptr, &OnTimerTick);
      
      std::this_thread::sleep_for(std::chrono::seconds(5));
    
      EXPECT_TRUE(mg_bTimerExpired);
    }
    

    Unfortunately OnTimerTick is never executed. Do you have an idea why?

    thx for your help :)


  • Lifetime Qt Champion

    @JuliusCaesar said in How to properly start Qt with Qt Eventloop for GTest:

    std::this_thread::sleep_for(std::chrono::seconds(5));

    You block the event loop here so no signals can be send.

    /edit: you're likely looking for QSignalSpy



  • bit this code is run from a new spawned QThread.
    Doesn't the Eventloop run in the main Thread()?

    I tried

    QThread* pCurrent = QThread::currentThread();
      pCurrent->sleep(5);
    

    as well. This does not work either
    Thx for the Tip with SignalSpy. I'll have a look at that :)


  • Lifetime Qt Champion

    @JuliusCaesar said in How to properly start Qt with Qt Eventloop for GTest:

    Doesn't the Eventloop run in the main Thread()?

    Every QThread has it's own eventloop when you don't overwrite run. See the documentation and objects create in a QThread have to use this eventloop. Otherwise a separate QThread would be useless.



  • Hey,
    thx for Your reply :)

    When I manually call QCoreApplication::processEvents() after the wait time, all pending events are processed.
    So this is a backup solution.

    But I do not want to call processEvents in each testcase.

    Is it possible to have 2 QThreads. One QThread with an working event which holds all QObjects and 1 where all Tests run in.
    Or is there any other solution? Could you help finding my figuring this out?

    How do non QT GUI applications process the event handling?

    thx :)
    greets


  • Lifetime Qt Champion

    @JuliusCaesar said in How to properly start Qt with Qt Eventloop for GTest:

    How do non QT GUI applications process the event handling?

    Exactly like the gui ones - the don't block the event loop and you should not do either. There is no reason for it.



  • alright thx for your answer. I'll have to think about an alternate solution :)


Log in to reply