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

I am writing some unit tests using QTest in Qt. I also came across a QBENCHMARK macro that compares the code that it encapsulates.

I run my unit tests and compare some of the code. QBENCHMARK reports how long it took to execute a method, and this is normal. I want to use runtime in unit test with, for example, QVERIFY2 (). How can i do this?

EDIT:

I am currently doing the following:

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); } } 
+6
source share
1 answer

From the documentation:

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

Sets the test result for this test function.

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

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

Note that the -iterations command line argument does not affect test functions without the QBENCHMARK macro.

You can also use the elapsed timer for such a thing.

+2
source

Source: https://habr.com/ru/post/970322/


All Articles