我在QTest写了一个小基准,尽管我使用过QBENCHMARK_ONCE。
这里有一些示例代码复制了这个问题:
头:
#ifndef MY_TEST_H
#define MY_TEST_H
#include <QtTest>
class MyTest : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void test1();
};
#endif // MY_TEST_H
cpp文件:
#include "mytest.h"
void MyTest::initTestCase() {
qDebug() << "init";
}
void MyTest::test1() {
QBENCHMARK_ONCE {
qDebug() << "bench";
}
qDebug() << "test1";
}
QTEST_MAIN(MyTest)
运行“mytest”我得到:
********* Start testing of MyTest *********
Config: Using QtTest library 5.5.1, Qt 5.5.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 5.3.1 20160407)
QDEBUG : MyTest::initTestCase() init
PASS : MyTest::initTestCase()
QDEBUG : MyTest::test1() bench
QDEBUG : MyTest::test1() test1
QDEBUG : MyTest::test1() bench
QDEBUG : MyTest::test1() test1
PASS : MyTest::test1()
RESULT : MyTest::test1():
0 msecs per iteration (total: 0, iterations: 1)
PASS : MyTest::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted
********* Finished testing of MyTest *********
我希望它只运行一次。一些基准测试每次迭代需要一分钟......
我在Linux上使用CMake和make后端。他们将测试类编译成单独的可执行文件。由于ctest没有给我有用的输出,我直接运行它们,即“#。/ mytest”
//增加: 的CMakeLists.txt
include(CTest)
enable_testing()
set(CMAKE_AUTOMOC ON)
find_package(Qt5Test REQUIRED)
add_executable(mytest "test/mytest.cpp")
add_test(mytest mytest)
target_link_libraries(mytest Qt5::Test)
答案 0 :(得分:2)
显然,这是为了减轻初始化和缓存对结果的影响的预期行为:
https://bugreports.qt.io/browse/QTBUG-12689
我认为他们应该将其添加到文档中。或者更容易混淆地命名宏。