GoogleTest

Submodule method (preferred)

To use this method, just checkout GoogleTest as a submodule:[^1]

  1. git submodule add --branch=release-1.8.0 ../../google/googletest.git extern/googletest

Then, in your main CMakeLists.txt:

  1. option(PACKAGE_TESTS "Build the tests" ON)
  2. if(PACKAGE_TESTS)
  3. enable_testing()
  4. include(GoogleTest)
  5. add_subdirectory(tests)
  6. endif()

I would recommend using something like PROJECT_NAME STREQUAL CMAKE_PROJECT_NAME to set the default for the PACKAGE_TESTS option, since this should only build by default if this is the current project. As mentioned before, you have to do the enable_testing in your main CMakeLists.

Now, in your tests directory:

  1. add_subdirectory("${PROJECT_SOURCE_DIR}/extern/googletest" "extern/googletest")

If you did this in your main CMakeLists, you could use a normal add_subdirectory; the extra path here is needed to correct the build path because we are calling it from a subdirectory.

The next line is optional, but keeps your CACHE cleaner:

  1. mark_as_advanced(
  2. BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS
  3. gmock_build_tests gtest_build_samples gtest_build_tests
  4. gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols
  5. )

If you are interested in keeping IDEs that support folders clean, I would also add these lines:

  1. set_target_properties(gtest PROPERTIES FOLDER extern)
  2. set_target_properties(gtest_main PROPERTIES FOLDER extern)
  3. set_target_properties(gmock PROPERTIES FOLDER extern)
  4. set_target_properties(gmock_main PROPERTIES FOLDER extern)

Then, to add a test, I’d recommend the following macro:

  1. macro(package_add_test TESTNAME)
  2. # create an exectuable in which the tests will be stored
  3. add_executable(${TESTNAME} ${ARGN})
  4. # link the Google test infrastructure, mocking library, and a default main fuction to
  5. # the test executable. Remove g_test_main if writing your own main function.
  6. target_link_libraries(${TESTNAME} gtest gmock gtest_main)
  7. # gtest_discover_tests replaces gtest_add_tests,
  8. # see https://cmake.org/cmake/help/v3.10/module/GoogleTest.html for more options to pass to it
  9. gtest_discover_tests(${TESTNAME}
  10. # set a working directory so your project root so that you can find test data via paths relative to the project root
  11. WORKING_DIRECTORY ${PROJECT_DIR}
  12. PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${PROJECT_DIR}"
  13. )
  14. set_target_properties(${TESTNAME} PROPERTIES FOLDER tests)
  15. endmacro()
  16. package_add_test(test1 test1.cpp)

This will allow you to quickly and simply add tests. Feel free to adjust to suit your needs. If you haven’t seen it before, ARGN is “every argument after the listed ones”. Modify the macro to meet your needs. For example, if you’re testing libraries and need to link in different libraries for different tests, you might use this:

  1. macro(package_add_test_with_libraries TESTNAME FILES LIBRARIES TEST_WORKING_DIRECTORY)
  2. add_executable(${TESTNAME} ${FILES})
  3. target_link_libraries(${TESTNAME} gtest gmock gtest_main ${LIBRARIES})
  4. gtest_discover_tests(${TESTNAME}
  5. WORKING_DIRECTORY ${TEST_WORKING_DIRECTORY}
  6. PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${TEST_WORKING_DIRECTORY}"
  7. )
  8. set_target_properties(${TESTNAME} PROPERTIES FOLDER tests)
  9. endmacro()
  10. package_add_test_with_libraries(test1 test1.cpp lib_to_test "${PROJECT_DIR}/european-test-data/")

Download method

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. Downloading a copy for each project is the recommended way to use GoogleTest (so much so, in fact, that they have disabled the automatic CMake install target), so this respects that design decision. This method downloads the project at configure time, so that IDEs correctly find the libraries. Using it is simple:

  1. cmake_minimum_required(VERSION 3.10)
  2. project(MyProject CXX)
  3. list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
  4. enable_testing() # Must be in main file
  5. include(AddGoogleTest) # Could be in /tests/CMakeLists.txt
  6. add_executable(SimpleTest SimpleTest.cu)
  7. add_gtest(SimpleTest)

Note: add_gtest is just a macro that adds gtest, gmock, and gtest_main, and then runs add_test to create a test with the same name:

  1. target_link_libraries(SimpleTest gtest gmock gtest_main)
  2. add_test(SimpleTest SimpleTest)

FetchContent: CMake 3.11

The example for the FetchContent module is GoogleTest:

  1. include(FetchContent)
  2. FetchContent_Declare(
  3. googletest
  4. GIT_REPOSITORY https://github.com/google/googletest.git
  5. GIT_TAG release-1.8.0
  6. )
  7. FetchContent_GetProperties(googletest)
  8. if(NOT googletest_POPULATED)
  9. FetchContent_Populate(googletest)
  10. add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR})
  11. endif()

[^1]: Here I’ve assumed that you are working on a GitHub repository by using the relative path to googletest.