You need to include the rest of your sources (car.cpp, motor.cpp and tires.cpp) in the build in some way. You can either add them along with main.cpp in the executable directly. The C CMake tools for Windows component uses the Open Folder feature to consume CMake project files (such as CMakeLists.txt) directly for the purposes of IntelliSense and browsing. Both Ninja and Visual Studio generators are supported. If you use a Visual Studio generator, it generates a temporary project file and passes it to msbuild.exe. C Quickstart With CMake. This document is designed to allow you to get the Abseil development environment up and running using CMake. We recommend that each person starting development with Abseil code at least run through this quick tutorial.
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.
I will assume here that the project structure follows the model described in a previous post:
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).
test directory contains a single executable allowing to test the
libfoo service using a mock version of
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
####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
ExternalProject 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.
###Writing a testfoo test program for libfoo
The testfoo program depends on libfoo, GoogleTest and GoogleMock.
Here is how the testfoo
CMakeLists.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.
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 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).
This file contains the declaration of the
FooTest class, which is the test fixture for the
Assuming the libbar library implements a public
Bar interface, we use GoogleMock to provide a fake implementation for test purposes only:
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
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::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
Please refer to the GoogleTest documentation for a much detailed presentation of how to create unit tests with Gtest.
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.
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