繁体   English   中英

使用CMake构建Qt测试

[英]Build Qt Tests with CMake

任何人都可以给我一些QT测试代码的例子和一个用Cmake构建并与CTest一起运行的CMakeLists.txt。 我好像找不到任何东西!

-Kurtis

以下是使用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"

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宏。 看看Charm,看看它的实际效果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM