2016-04-28 105 views
1

我已经在QTest中编写了一个小基准,尽管我已经使用了QBENCHMARK_ONCE。QTest执行测试用例两次

这里是一些示例代码复制问题:

头:

#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进行后端处理。他们测试的类被编译成单独的可执行文件。由于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

您可以尝试创建一个小的可重现示例吗?例如,哪个值分配给NUMBER_ITERATIONS在您的示例中不可见。 – KimKulling

+0

我会尝试创建一个。 NUMBER_ITERATIONS并不重要,它只是compute函数的一个参数,它定义了内部循环(迭代算法)运行的频率。它是10000 btw。 ;) –

回答