使用CMake构建Qt测试

任何人都可以给我一些QT测试代码的例子和一个用Cmake构建并与CTest一起运行的CMakeLists.txt。我好像找不到任何东西! -Kurtis     
已邀请:
从Charm(Tests / CMakeLists.txt)中获取的示例:
SET( TestApplication_SRCS TestApplication.cpp )
SET( TEST_LIBRARIES CharmCore ${QT_QTTEST_LIBRARY} ${QT_LIBRARIES} )

SET( SqLiteStorageTests_SRCS SqLiteStorageTests.cpp )
QT4_AUTOMOC( ${SqLiteStorageTests_SRCS} )
ADD_EXECUTABLE( SqLiteStorageTests ${SqLiteStorageTests_SRCS} )
TARGET_LINK_LIBRARIES( SqLiteStorageTests ${TEST_LIBRARIES} )
ADD_TEST( NAME SqLiteStorageTests COMMAND SqLiteStorageTests )
与普通可执行文件的唯一区别是您调用ADD_TEST宏。 看看例如魅力在行动中看到它。     
以下是使用cmake 2.8.11和Qt5.2的示例。请注意,cmake现在支持底部带有.moc-include的testfiles。 的CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.11)
project(foo)

enable_testing()

# Tell CMake to run moc when necessary:
set(CMAKE_AUTOMOC ON)

# As moc files are generated in the binary dir, tell CMake
# to always look for includes there:
set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(Qt5Test REQUIRED)

add_executable(foo foo.cpp)
add_test(foo foo)

target_link_libraries(foo Qt5::Test)
Foo.cpp中:
#include <QTest>

class Foo : public QObject {
    Q_OBJECT
private slots:
    void t1() { QVERIFY(true); }
};

QTEST_MAIN(Foo)
#include "foo.moc"
    

要回复问题请先登录注册