任何人都可以给我一些QT测试代码和CMakeLists.txt的示例,它使用Cmake构建并与CTest一起运行。我好像找不到任何东西!
-Kurtis
答案 0 :(得分:30)
以下是使用cmake 2.8.11和Qt5.2的示例。请注意,cmake现在支持底部带有.moc-include的测试文件。
的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"
答案 1 :(得分:12)
取自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宏。 看看例如看到它在行动中的魅力。