任何人都可以给我一个QT测试代码和Cmakelists.txt的示例,并用Cmake构建并与CTEST一起运行。我似乎找不到任何!

-Kurtis

有帮助吗?

解决方案

取自的例子 魅力 (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宏。看看EG魅力以查看它的作用。

其他提示

这是使用的示例 CMAKE 2.8.11 和QT5.2。请注意,Cmake现在以.moc-包含的底部支持测试文件。

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"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top