Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use QTest QBENCHMARK macros result as a parameter in a unit test

I am writing some unit tests using QTest in Qt. I also encountered the QBENCHMARK macro, which benchmarks the code it encapsulates.

I am running my unit tests and benchmarks some of the code. The QBENCHMARK reports how long it took to execute some method and that is fine. I want to use the execution time in a unit test with for example QVERIFY2(). How can I do this?

EDIT:

What I am currently doing is:

void UnitTest::benchmark()
{
    QString str1 = QLatin1String("This is a test string");
    QString str2 = QLatin1String("This is a test string");

    QCOMPARE(str1.localeAwareCompare(str2), 0);

    QBENCHMARK {
        str1.localeAwareCompare(str2);
    }
}
like image 281
uniquenamehere Avatar asked Jun 04 '14 15:06

uniquenamehere


1 Answers

From the documentation:

void QTest::setBenchmarkResult(qreal result, QBenchmarkMetric metric)

Sets the benchmark result for this test function to result.

Use this function if you want to report benchmark results without using the QBENCHMARK macro. Use metric to specify how Qt Test should interpret the results.

The context for the result will be the test function name and any data tag from the _data function. This function can only be called once in each test function, subsequent calls will replace the earlier reported results.

Note that the -iterations command line argument has no effect on test functions without the QBENCHMARK macro.

You could also of course just use an elapsed timer for such a thing.

like image 148
lpapp Avatar answered Nov 18 '22 07:11

lpapp