Search code examples
c++unit-testingqtqttest

How much does each test case consume in QTest for C++


Is there a simply way to knowing how much time consume each test case in QT Test Framework for C++?

It will be incredible helpful to getting some metrics.


Solution

  • You could write a custom Timer class which starts a usually monotonic elapsed timer on creation and prints the elapsed time on deletion (see QElapsedTimer::elapsed()):

    timer.h:

    #include <QElapsedTimer>
    class Timer {
    public:
        Timer() {
            timer.start();
        }
        ~Timer() {
            qint64 ms = timer.elapsed();
            qDebug("Time needed: %s ms%s", ms,
                   timer.isMontonic() ? "" : " (WARNING: non-monotonic)");
        }
    private:
        QElapsedTimer timer;
    }
    

    The benefit of using QElapsedTimer over QTime is that Qt will try to use the best available monotonic timer on each platform. QTime is not guaranteed to be montonic: it will decrease when time synchronization daemon/service adjusts the clock, etc.

    Now insert the line Timer t; at the beginning of each test case you want to measure the time of; no more code. This simply creates a Timer object, which starts the internal timer, and deletes the object when it gets out of scope (which is at the end of the method) and thus prints the elapsed time:

    Your test case (.cpp):

    #include "timer.h"
    ...
    void TestClass::myTestCase1()
    {
        Timer t; //the only line needed to add to your test case
    
        ... //the code for test case
    }
    void TestClass::myTestCase2()
    {
        Timer t; //the only line needed to add to your test case
    
        ... //the code for test case
    }
    ...