Googletest Cmake

  1. Cmake Google Test Example
  2. Googletest Cmake Fetchcontent Example
  3. Google Test Cmake
  • Locate the Google C Testing Framework. New in version 3.20: Upstream GTestConfig.cmake is used if possible.
  • Googletest folder has its own CMakeLists.txt file and include/ and src/ folders (no need to store full googletest repo!). Tree googletest/ googletest/ ├── CMakeLists.txt ├── include │ └── gtest │ ├── gtest-death-test.h. └── src ├── gtest-all.cc ├── gtest.cc └── gtestmain.cc.

This means that any time a test source file is change, CMake must be re-run. For some projects, this may consume a significant amount of time. Most of the problems, however, stem from the manner in which tests are found. Since CMake is not a C compiler, gtestaddtests finds tests using regular expressions. This approach is fairly simple,. Googletest Samples. If you’re like us, you’d like to look at googletest samples. The sample directory has a number of well-commented samples showing how to use a variety of googletest features. Sample #1 shows the basic steps of using googletest to test C functions. You can use the downloader in my CMake helper repository, using CMake's include command. This is a downloader for GoogleTest, based on the excellent DownloadProject tool.

Continuous integration requires a robust test environment to be able to detect regressions as early as possible.

A typical test environment will typically be composed of integration tests of the whole system and unit tests per components.

This post explains how to create unit tests for a C++ component using GoogleTest and CMake.

##Project structure

I will assume here that the project structure follows the model described in a previous post:

The main subdirectory contains the main project target, an executable providing the super-useful libfoo service using the awesome libbar backend (for example libfoo could be a generic face recognition library and libbar a GPU-based image processing library).

The test directory contains a single executable allowing to test the libfoo service using a mock version of libbar.

From Wikipedia: In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways.

For those interested, the code for this sample project is on github.

##A closer look at the test directory

In my simplistic example, there is only one subdirectory under test, but in a typical project, it would contain several subdirectories, one for each test program.

Tests programs are based on Google’s Googletest framework and its GoogleMock extension.

Since all test programs will be using these packages, the root CMakeLists.txt file should contain all directives required to resolve the corresponding dependencies. This is where things get a bit hairy, since Google does not recommend to install these packages in binary form, but instead to recompile them with your project.

###Resolving GoogleTest and GoogleMock dependencies

There are at least three options to integrate your project with GoogleTest and GoogleMock.

####Having both packages integrated in your build system

Obviously, this is only an option if you actually do have a buildsystem, but if this is the case, this would be my recommendation.

Depending on how your buildsystem is structured, your mileage may vary, but in the end you should be able to declare GoogleTest and GoogleMock as dependencies using CMake functions like the built-in find_package or the pkg-config based pkg_check_modules.

####Add both packages sources to your project

Adding the GoogleTest and GoogleMock sources as subdirectories of test would allow you to compile them as part of your project.

This is however really ugly, and I wouldn’t recommend you doing that …

####Add both packages as external CMake projects

According to various answers posted on StackOverflow, this seems to be the recommended way of resolving GoogleTest and GoogleMock dependencies on a per project basis.

It takes advantage of the CMakeExternalProject module to fetch GoogleTest and GoogleMock sources from the internet and compile them as third-party dependencies in your project.

Below is a working example, with a few comments explaining what’s going on:

Note: It should theoretically be possible to set the GoogleTest and GoogleMock include directories as target properties using the INTERFACE_INCLUDE_DIRECTORIES variable, but it fails because these directoires don’t exist yet when they are declared. As a workaround, I had to explicitly use include_directories to specify them.

Cmake Google Test Example

###Writing a testfoo test program for libfoo

The testfoo program depends on libfoo, GoogleTest and GoogleMock.

Here is how the testfooCMakeLists.txt file would look like:

The libraries required for the build are listed under target_link_libraries.CMake will then add the appropriate include directories and link options.

The testfoo program will provide unit tests for the Foo class of the libfoo library defined below.

####foo.h

####foo.cpp

The sample Test program described in the GoogleTest Documentation fits in a single file, but I prefer splitting the Unit Tests code in three types of files.

###main.cpp

The main.cpp file will contain only the test program main function.This is where you will put the generic Googletest Macro invocation to launch the tests and some initializations that need to be put in the main (nothing in this particular case).

###testfoo.h

This file contains the declaration of the FooTest class, which is the test fixture for the Foo class.

Googletest cmake fetchcontent example

###mockbar.h

Assuming the libbar library implements a public Bar interface, we use GoogleMock to provide a fake implementation for test purposes only:

Googletest Cmake Fetchcontent Example

This will allow us to inject controlled values into the libfoo library when it will invoke the Bar class methods.

Please refer to the GoogleMock documentation for a detailed description of the GoogleMock features.

###testfoo.cpp

This file contains the implementation of the TestFoo fixture class.

This is where the actual tests are written.

We will test the output of the Foo::baz() method, first having default values for the Bar::qux() and Bar::norf() methods returned by our mock, then overrding the value returned by Bar::norf() with a value specific to our test.

In all test cases, we use GoogleTest expectations to verify the output of the Foo::baz method.

Please refer to the GoogleTest documentation for a much detailed presentation of how to create unit tests with Gtest.

##Building tests

As usual, it is recommended to build your program out-of-tree, ie in a directory separated from the sources.

First, you need to invoke the cmake command to generate the build files.

This should produce an output similar to this one:

Then, build the project targets.

The following output corresponds to the case where GoogleTest and GoogleMock are automatically fetched from their repositories and built as third-party dependencies.

##Running tests

Once the test programs have been built, you can run them individually …

… producing a detailed output …

Note: You can get rid of GoogleMock warnings by using a nicemock.

… or globally through CTest

… producing only a test summary.

Please enable JavaScript to view the comments powered by Disqus.comments powered by Disqus

This post is about how to cross-compile GoogleTest in Eclipse.

Environment
  • Ubuntu 16.04 LTS
  • Eclipse Neon.3 Release 4.6.3
Install CMake
Checkout GoogleTest
Configure ARM toolchain

How to install ARM toolchain see Raspberry Pi: C++ cross-compiling.

Cmake
  • Create in the root of GoogleTest folder toolchain-arm-linux-gnueabihf.cmake file with the following content:
  • Configure CMake:
Create Eclipse project

Import the project in Eclipse:

  • File > Import
  • Select:
    • General
    • Existing Project into Workspace
    • Next
  • Select project
    • Select root directory > Browse (to the GoogleTest folder)
    • Finish
Compile GoogleTest

Eclipse:

Project > Build project

Google Test Cmake

Check library type