有人能给我提供一些 QT 测试代码的示例以及一个能够使用 Cmake 构建并使用 CTest 运行的 CMakeLists.txt 文件吗?我似乎找不到任何相关资源!
-Kurtis
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以查看其运作方式。
set(CMAKE_AUTOMOC ON)
。 - Cyrille MASCART