Search code examples
qtcmakectestqtestlibqtest

Registering tests on QTest in CTest apart from executable like GTest realizations


I have an executable with multiple tests written with QTest.
The executable is being registered using add_tests(NAME test_name COMMAND test_executable). Obviously, tests will be handled as one in CTest, so it will be difficult to find the problem if one of the hundreds fails. It will be worse when new executables with tests are added.

Is it possible to register the tests separately like with GTest realizations? Is there an alternative to GTEST_ADD_TESTS or GTEST_DISCOVER_TESTS CMake functions for QTest tests?


Solution

  • Just with using QTest I didn't find a solution but tried a different approach that works.

    I changed testing framework to GTest. But still, a test application is linked with QTest library for having access to such features as emulating GUI interaction.

    The default main function from the gtest_main module may be not compatible because it is required require to initialize QApplication for many features of Qt. There is an implementation of main based on the GTest's in the gtest_main.cc source file.

    #include <cstdio>
    #include <gtest/gtest.h>
    
    #include <QApplication>
    
    int main(int argc, char ** argv)
    {
        QApplication app(argc, argv);
        printf("Running main() from %s\n", __FILE__);
        testing::InitGoogleTest(&argc, argv);
        return RUN_ALL_TESTS();
    }
    

    After testing, I didn't find any problem as well as using GTEST_ADD_TESTS or GTEST_DISCOVER_TESTS CMake functions.