Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build Qt Tests with CMake

Tags:

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

like image 432
Kurtis Nusbaum Avatar asked Jan 20 '11 23:01

Kurtis Nusbaum


People also ask

Does Qt use CMake?

Introduction. CMake can find and use Qt 4 and Qt 5 libraries. The Qt 4 libraries are found by the FindQt4 find-module shipped with CMake, whereas the Qt 5 libraries are found using "Config-file Packages" shipped with Qt 5.

How do I add CMake to Qt?

To add a path to a CMake executable that Qt Creator does not detect automatically, and to specify settings for it, select Add. To make changes to automatically detected installations, select Clone. Qt Creator uses the default CMake if it does not have enough information to choose the CMake to use.


2 Answers

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" 
like image 91
Daniel Näslund Avatar answered Oct 07 '22 01:10

Daniel Näslund


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.

like image 24
Frank Osterfeld Avatar answered Oct 07 '22 00:10

Frank Osterfeld