2
votes

This should be a fairly simple question, but given the black arts of project structuring using cmake, it will help quite a bit of people struggling with this.

I'm trying to get my codebase a little bit more organized. For this, I'm creating subfolders that contain the test suites according to their domain.

Google test itself is already compiling and running, the only thing is that with this restructure, Google Test can't find any of the Test Cases I have.

Here is my structure:

tests\
     |
     \domain1\
             |CMakeLists.txt
             |domain1_test.cpp
             |domain1_test.hpp
             |[.. more tests ...]
     \domain2\
             |CMakeLists.txt
             |domain2_test.cpp
             |domain2_test.hpp
             |[.. more tests ...]
     |main.cpp
     |CMakeLists.txt

As you can see, I have two folders where tests live.

The CMakeLists.txt files in those are as follows:

SET(DOMAIN1_TEST_SRC
        domain1_test.cpp
        domain1_test.hpp)

ADD_LIBRARY(domain1testlib STATIC ${DOMAIN1_TEST_SRC})

TARGET_LINK_LIBRARIES(domain1testlib
        ${Boost_LIBRARIES}
        domain_lib
        gtest
        )

TARGET_INCLUDE_DIRECTORIES(domain1testlib
        INTERFACE
        ${CMAKE_CURRENT_SOURCE_DIR})

The CMakeLists.txt in the main tests directory is:


add_subdirectory(domain1)
add_subdirectory(domain2)

ADD_EXECUTABLE(my_domain_tests main.cpp)

TARGET_LINK_LIBRARIES(my_domain_tests
        ${Boost_LIBRARIES}
        domain1testlib
        domain2testlib
        comptestlib
        gtest
        )

add_test(MyTestSuite my_domain_tests)

What am I doing wrong?

Running tests just says that No tests were found.

Thanks!

UPDATE Adding my main.cpp

It's really nothing special, just the boilerplate main.cpp file.

#include "gtest/gtest.h"

int main(int argc, char ** argv) {

    ::testing::InitGoogleTest(&argc, argv);

    return RUN_ALL_TESTS();
}
2
A few things to check. What's in your main.cpp? Do you need it or can you link against gtest_main instead of gtest, allowing gtest itself to work out what tests are available to run? Does your top level CMakeLists.txt have enable_testing() called somewhere?Craig Scott
I added the main.cpp file I'm using.Yes, my top level CMakeLists.txt has the enable_testing() call.Hector Villarreal

2 Answers

1
votes

The problem is that no symbols from your domain*testlibs are referenced from your executable sources, i.e. main.cpp.

The TEST and TEST_F macros in Google Test automatically register the test cases with the test runner. So your test source files are the ones that actually include references to symbols in the gtest library and not the other way around. Thus, the linker will not include any of your actual test cases.

You should include the domain*_test.cpp and domain*_test.hppfiles as part of your executable sources instead of creating libraries with them. You can do that directly referencing the files or using a variable defined in each CMakeLists.txt with the list of sources.

-1
votes

What am I doing wrong?

Running tests just says that No tests were found.

to make available for "make test" and to work ctest inside build dir you need

  ENABLE_TESTING()# Force "make test" to works

before add_test in CMakeLists.txt