Build Qt Tests with CMake
Asked Answered
R

2

32

Can anyone give me an example of some QT test code and a CMakeLists.txt that build with Cmake and ran with CTest. I can't seem to find any!

-Kurtis

Rettke answered 20/1, 2011 at 23:26 Comment(0)
A
12

An example taken from 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 )

The only difference to a normal executable is that you call ADD_TEST macro. Have a look at e.g. Charm to see it in action.

Alcine answered 21/1, 2011 at 9:11 Comment(3)
Hopefully this will save someone some time further down the road - according to this page: "Note that CMake can not handle qtest files with a moc include at the bottom."Harlamert
If that's the case, you'd have to remember to automoc your test files as well.Ioved
For moc and CMake, use set(CMAKE_AUTOMOC ON)Michellemichels
L
42

Here is an example of using cmake 2.8.11 and Qt5.2. Note that cmake now supports testfiles with a .moc-include at the bottom.

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"
Lecithinase answered 9/5, 2014 at 8:48 Comment(3)
I had to edit the last line of the CMakeList to compile the code: target_link_libraries(foo Qt5::Widgets Qt5::Test). +1 for qt5. I'm not sure if it's me, or is in fact an error, so I'm not editing the answer.Frodeen
@cauchy: well, of course if your test requires QtWidgets, it needs to be linked in... :PPeaceful
@Daniel, I am still getting error for "foo.moc" file (~ no such file ). I am using CMake 3 version & Qt 5.9.True
A
12

An example taken from 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 )

The only difference to a normal executable is that you call ADD_TEST macro. Have a look at e.g. Charm to see it in action.

Alcine answered 21/1, 2011 at 9:11 Comment(3)
Hopefully this will save someone some time further down the road - according to this page: "Note that CMake can not handle qtest files with a moc include at the bottom."Harlamert
If that's the case, you'd have to remember to automoc your test files as well.Ioved
For moc and CMake, use set(CMAKE_AUTOMOC ON)Michellemichels

© 2022 - 2024 — McMap. All rights reserved.