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

Unit Testing with GTest



  • The company I am writing code for uses GTest in their CI/CD. I'd really rather not bring in QTest if at all possible.

    However, GTest requires you to run their test runner on the main thread:

    int main(int argc, char* argv[])

    {
        ::testing::InitGoogleTest(&argc, argv);
        return RUN_ALL_TESTS();
    }
    

    So does Qt Application, which seems necessary for everything in Qt, even the websocket code I want to test.

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        // SNIP
        
        EchoServer *server = new EchoServer(port, debug);
        QObject::connect(server, &EchoServer::closed, &a, &QCoreApplication::quit);
    
        return a.exec();
    }
    

    So, how would I go about testing the class I made based on the echo sever example
    https://code.qt.io/cgit/qt/qtwebsockets.git/tree/examples/websockets/echoserver?h=5.14

    in Gtest?

    I've even tried using the wizard in Creator according to the instructions at:
    https://doc.qt.io/qtcreator/creator-autotest.html
    and the code it generates also does not start a QApplication, so anything that needs QApplication running cannot be tested, unless I am missing something.


Log in to reply