Travis: CMake, Qt5 and Tests

It took me a while until I understood howto setup travis-ci to build and test Solstice. So here are the steps I took and the difficulties I encountered.

The Application

The Application is written in C++ and uses QT5.5 as GUI Framework. The build tool is CMake. Unit Tests will be using google-test to test the c++ core and qml Tests will test the frontend. The project is hosted in GitHub.

Project Structure

I followed this post by David Corvoysier to find a good structure to integrate CMake and google-test. There are however a few changes. The test directory has several sub directories: one for each C++ Module and one named qmltests, which contains all GUI tests.

wose:Solstice/ (master) $ tree .
.
├── cmake
│   └── modules
│       └── QmlTest.cmake
├── CMakeLists.txt
├── LICENSE
├── qml
│   ├── CMakeLists.txt
│   ├── images
│   │   └── menu.svg
│   ├── main.qml
│   ├── qml.qrc
│   ├── RingMenu.qml
│   └── SettingsDialog.qml
├── README.org
├── src
│   ├── CMakeLists.txt
│   └── main.cpp
└── tests
    ├── CMakeLists.txt
    ├── core
    └── qmltests
        ├── CMakeLists.txt
        └── tst_QmlTestTest.qml

google-test

google-tests worked without any issues. CMake will fetch the corresponding archive and adds gtest and gmock as external projects. The following CMakeLists.txt is an example of one which can be used in any of the tests/[modulename] directories.

 1: file(GLOB SRCS *.cpp)
 2: 
 3: ADD_EXECUTABLE(testfoo ${SRCS})
 4: 
 5: TARGET_LINK_LIBRARIES(testfoo
 6:     libgtest
 7:     libgmock
 8: )
 9: 
10: add_test(NAME testfoo
11:          COMMAND testfoo)

Here I compile all *.cpp files 1 to an executable named testfoo 3. Link it against libgtest and libgmock 5. And finally tell CMake that testfoo is a test also named testfoo 10.

I used the following test to verify that c++ tests are working.

#include "gtest/gtest.h"

int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    int ret = RUN_ALL_TESTS();
    return ret;
}

TEST(FactorialTest, Zero) {
  EXPECT_EQ(1, 1);
}

qml Tests

The CMake module cmake/QmlTest.cmake is stolen from playground-qtmirserver. It provides a function add_qml_test to simply add qml tests to the test suite. The following CMakeLists.txt is from tests/qmltests.

include(QmlTest)

add_qml_test(. QmlTestTest)

travis-ci

As I started reading on howto use travis I learned about howto use the new addon feature to add all needed repositories and install the required software. After some tests I figured out that the repositories and packages have to be whitelisted to be able to use them. As of this writing either the repository nor the qt5.5 packes are on those lists. A corresponding issue is open, it'll probably just take a little more time.

So I switched back to the legacy mode and got the project to compile. But the qml tests failed for OTHER_FAULT. I first thought missing qt packages could be the reason and added the qt55quickcontrols package to the installation. No change. Turns out the qml tests need a valid DISPLAY environment variable. To achieve this I started xvfb within the before_install section 6.

 1: language: cpp
 2: 
 3: compiler:
 4:   - gcc
 5: 
 6: before_install:
 7:   - export DISPLAY=:99.0
 8:   - sh -e /etc/init.d/xvfb start
 9: 
10: install:
11:   - sudo apt-add-repository -y ppa:ubuntu-toolchain-r/test
12:     && sudo apt-add-repository -y ppa:beineri/opt-qt55
13:   - sudo apt-get -qq update
14:   - sudo apt-get -qq install g++-4.9 qt55base qt55quickcontrols qt55declarative qt55tools qt55svg lcov curl
15:   - export CXX="g++-4.9"
16:     && export CC="gcc-4.9"
17:   - source /opt/qt55/bin/qt55-env.sh
18:   - wget -qO- http://www.cmake.org/files/v3.1/cmake-3.1.0-Linux-x86_64.tar.gz | tar xvz
19:   - sudo cp -fR cmake-3.1.0-Linux-x86_64/* /usr
20: 
21: before_script:
22:   - mkdir build
23:   - cd build
24:   - cmake -DCMAKE_BUILD_TYPE=debug -DCMAKE_PREFIX_PATH=/opt/qt55/lib/cmake ..
25: 
26: script: make && make test
27: 
28: notifications:
29:   email: false

I manually fetch and install cmake to get a newer version (>=3.1) 18.

All of the above is included here and can probably be used as some sort of boilerplate for similar projects.

October 07, 2015 | Travis, CMake, c++11, Qt5, TDD
about | blog | archives