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

QTest, how to aggregate all my unit tests in one main test?



  • Hi,
    Is there a way to run all the unit tests from one main test project?
    I thought I could do this kind of thing in the main.cpp

    #include <QtTest/QtTest>
    #include "Test1.h"
    #include "Test2.h"
    
    QTEST_MAIN(Test1)
    #include "moc_Test1.cpp"
    
    QTEST_MAIN(Test2)
    #include "moc_Test2.cpp"
    

    but no, in fact, QT_MAIN redefine the main....

    So I've done this for now:

    #include "Test1.h"
    #include "Test2.h"
    #include "moc_Test1.cpp"
    #include "moc_Test2.cpp"
    #include <QTest>
    int main(int argc, char *argv[])
    {
        QTEST_SET_MAIN_SOURCE_PATH
    
        Test1 t1;
        QTest::qExec(&t1, argc, argv);
    
        Test2 t2;
        QTest::qExec(&t2, argc, argv);
        return 0;
    }
    

    The "problem" is that the test results are not together. Is there a way to aggregate them?
    I'm having this at the moment:

    ********* Start testing of Test1 *********
    Config: Using QtTest library 5.10.1, Qt 5.10.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 5.3.1 20160406 (Red Hat 5.3.1-6))
    PASS   : Test1::initTestCase()
    PASS   : Test1::test_cache_withoutDeleteQueue()
    PASS   : Test1::test_cache_deleteQueue()
    PASS   : Test1::cleanupTestCase()
    Totals: 4 passed, 0 failed, 0 skipped, 0 blacklisted, 4640ms
    ********* Finished testing of Test1 *********
    ********* Start testing of Test2 *********
    Config: Using QtTest library 5.10.1, Qt 5.10.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 5.3.1 20160406 (Red Hat 5.3.1-6))
    PASS   : Test2::initTestCase()
    PASS   : Test2::test_createActiveRequests()
    PASS   : Test2::cleanupTestCase()
    Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted, 0ms
    ********* Finished testing of Test2 *********
    

    I'd like to be able to generate the sum up at the end, something like this:
    Number of Tests: 2
    Number of Tests Passed: 2
    Number of Tests Failed: 0
    (Total number of unit tests: 7)

    And in case there are some Test Failed being able to give the list



  • Ok in fact the return of QTest::qExec kind of give the number of Failed Unit Tests.
    So we can do manually something like this:

    int main(int argc, char *argv[])
    {
        QTEST_SET_MAIN_SOURCE_PATH
    
        QList<QString> failedTests;
        ushort nbTests = 0, nbTestsOK = 0, nbTestsKO = 0;
        uint nbFailedUnitTests = 0;
    #ifdef __Launch_Test1__
        {
            Test1 test;
            int res = QTest::qExec(&test, argc, argv);
            qDebug() << "[Test1] QTest::qExec return: " << res;
            if (res == 0)
                ++nbTestsOK;
            else
            {
                ++nbTestsKO;
                failedTests << "Test1";
            }
            ++nbTests;
            nbFailedUnitTests += res;
        }
    #endif
    #ifdef __Launch_Test2__
        {
            Test2 test;
            int res = QTest::qExec(&test, argc, argv);
            qDebug() << "[Test2] QTest::qExec return: " << res;
            if (res == 0)
                ++nbTestsOK;
            else
            {
                ++nbTestsKO;
                failedTests << "Test2";
            }
            ++nbTests;
            nbFailedUnitTests += res;
        }
    #endif
        qDebug() << "\n\n\n\n";
        qDebug() << "###############################################";
        qDebug()  << "# Overall Tests Results:";
        qDebug() << "#     - number of macro Tests: " << nbTests;
        qDebug() << "#     - number of macro Tests OK: " << nbTestsOK;
        qDebug() << "#     - number of macro Tests FAILED: " << nbTestsKO;
        if (!failedTests.isEmpty())
        {
            qDebug() << "#     - number of Unit Tests FAILED: " << nbFailedUnitTests;
            qDebug() << "# List of FAILED macro Tests: " << failedTests.join(", ");
        }
        qDebug() << "###############################################";
    
        return 0;
    }
    

    we just can't have the global number of unit tests that have been run...
    bit a shame but I'll live with it ;)


Log in to reply