1

My project is organized like this:

  • cpp
    • main.cpp (calls code from dataStructures/ and common/)
    • CMakeLists.txt (topmost CMakeLists file)
    • build
    • test
      • main.cpp
      • CMakeLists.txt
      • common
        • CMakeLists.txt
        • include
        • src
      • googletest
      • build
    • common
      • CMakeLists.txt (should be responsible for building common shared library)
      • include
        • utils.h
      • src
        • utils.cpp
      • build

build\ directories contain the built target. The actual code can be seen here: https://github.com/brainydexter/PublicCode/tree/master/cpp

I'm able to build shared libraries for the actual code ( like common). Now, I'd like to test it using GoogleTest. So, I created a test directory and put the test code in there (what I have in the repo is just a sample, more like hello world to get the build system up and working. I will be adding more test cases later on).

I need some help in triggering the tests from the topmost CMakeLists.txt. I'm not sure how to do this. In test, CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.1.2)

add_subdirectory(googletest/googletest)
include_directories(googletest/googletest/include)

add_subdirectory(common)

enable_testing()

add_executable(testCpp main.cpp)
target_link_libraries(testCpp gtest gtest_main )

Do I need to add anything else to enable tests in test/common ?

test/common/CMakeLists.txt:

file(GLOB SOURCES "src/*.cpp")

add_executable(testCommon ${SOURCES})

target_link_libraries(testCommon cppCommon)

I'm not sure if the target should be executable here or a library that the google test framework can consume ? I'd appreciate any help on this.

1

1 Answer 1

0

You may find the FindGTest module helpful. Since the location of gtest is fixed, you can do something along these lines to find it:

set (GTEST_ROOT ${CMAKE_SOURCE_DIR}/googletest)
find_package (GTest required)

As for whether you need an executable, this module defines the GTEST_MAIN_LIBRARIES variable, so that when you link your tests, you simply need to do:

target_link_libraries(testCommon ${GTEST_MAIN_LIBRARIES})

The resulting binary will use the default gtest main function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.