Unit Test Output & Project Structure Advice --- CMake + Google Test Framework

Landon picture Landon · Aug 31, 2011 · Viewed 13.9k times · Source

I'm new to CMake and I'm using the Google Test Framework. I've looked for more complex examples of combining CMake and the Google testing framework, but I've not turned up much luck. I was hoping that someone could provide their opinion on the setup I've arrived at and answer a question for me. Here's the basic project structure:

ProjectFolder
-CMakeLists.txt
/build
/include
  -all my .h files
/src
  -CMakeLists.txt
  -all my .cpp files
/tests
  -CMakeLists.txt
  /gtest
    -Google's CMakeLists.txt
  /mocks
    -CMakeLists.txt
    -cpp and h files for mocks

In a nutshell the root cmake file adds src and tests as subdirectories. The src cmake file globs the cpp files into a library. The tests cmake file adds mocks and gtest as subdirectories. The mock cmake file globs all of its cpp files into a second library. Finally the cmake file in the tests folder links the src library, google libraries, and the mocks libraries with the unit test executables.

What I eventually get are several executables: objA_unittest, objB_unittest, etc

A few things:

  1. To build all of this and still keep my project directory fairly clean I cd into the build folder and run "cmake .." This seems to work really well and I was planning on configuring vim to always run make out of this folder. Does anyone have any tips or or see any problems with this approach?

  2. I'm not fond of the idea of having to nest the google library in my project folder (particularly if I eventually have several projects that use it), but as it seems to be highly suggested that you not precompile the google libraries I can see no way around this. However, if anyone does know of a better way, I would love to hear it.

  3. Running the tests. I like that all I have to do is run "make test" to execute all of my unit tests with ctest. However, I've discovered that I don't like the output very much. Each unit test executable contains several different unit tests. Running these directly provides Google's very verbose feedback. If one of the tests in an executable fails I know exactly what test fails and why. Previously, I used a makefile and at the end of the makefile I simply executed all of the tests. I first attempted to fix this by creating a custom build target in cmake. However, it only executes the first command. I either need a way to get more verbose output from CTest OR I need a way to automatically output a bash script or something similar from CMake to execute each unit test executable. In other words, I have the cmake file set up now so I only have to add the unit test in one place to generate the executable, and I don't want to have to remember to add it to a second place if I can avoid it. Does anyone have any experience that would be helpful in this point?

Thanks very much in advance.

Answer

Sergei Nikulov picture Sergei Nikulov · Sep 1, 2011

Here the my example. I've made it as short guideline for my dev team. Perhaps you'll find it useful also.