Landon
Landon

Reputation: 561

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

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.

Upvotes: 21

Views: 15119

Answers (2)

Sergei Nikulov
Sergei Nikulov

Reputation: 5110

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

Upvotes: 12

Andrey Kamaev
Andrey Kamaev

Reputation: 30122

  1. It is actually a recommended cmake practice called out-of-source build
  2. AFAIK it is not recommended to install recompiled googletest libraries in the system. So there should not be any problem if you compile it as shared or static library as part of your project build. I have used googletest 1.6.0 this way without problems on Windows, Linux, OSX and Android.
  3. I'm not sure about CTest but custom target is definitely able to run all tests for you. Here is a short solution I can suggest:

Add following lines into your top level CMakeLists.txt (before adding any tests):

add_custom_target(test)
macro(run_test test_target)
  add_custom_target(${test_target}_runtest
      COMMAND ${test_target} #cmake 2.6 required
      DEPENDS ${test_target}
      WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
  add_dependencies(test ${test_target}_runtest)
endmacro()

Next for each test add a single line to append your test to the test target:

#add_executable(mytest ${mysources})
run_test(mytest)

Upvotes: 11

Related Questions