I am working on a library. That library internally is broken into folders with related modules. Each folder has its own unit tests. At the root of the library I want to make a single CMakeLists.txt file that builds all the unit tests for the entire library. Ideally I dont want to directly reference every source file in the main CMakelists.txt, but instead include each of the modules tests.
So, in a simplified form:
Library
Module1
Inc
Module1.hpp
Src
Module1.cpp
Test
Module1Tests.cpp
CMakeLists.txt
Module2
... (same pattern as above)
Test
main.cpp
CMakeLists.txt
In the main CMakeLists.txt I had (roughly):
add_executable(LibTests main.cpp)
add_subdirectory(Module1)
add_subdirectory(Module2)
target_link_libraries(LibTests Module1)
target_link_libraries(LibTests Module2)
And in each modules CMakelists.txt (again roughly)
add_library(Module1)
target_sources(Module1.cpp)
This works fine and everything compiles nicely. It generates for Visual Studio solution for the library, with a project for LibTests, Module1 and Module2. Allows compiling of just one module when working on tests and only shows the source files relevant to those tests. Quite nice I thought!
But, as the modules are generated as libraries, the code is never actually referenced, so the tests are not executed or referenced by VSs test explorer.
An alternate would be to (cmake) include the modules rather than adding them as sub_directories, but that does not generate nice independent VS projects for them.
So, is there a way to build this test setup so that I can have it nicely laid out in visual studio projects, or am I trying to have my cake and eat it too? Also open to suggestions for a better structure for testing.