How to run test in subdirectories that are sub projects

This is my structure

MyApp
├── CMakeLists.txt
├── src
│   ├── CMakeLists.txt
│   ├── config
│   ├── controller
└── tests
    ├── CMakeLists.txt
    ├── config
    │   ├── CMakeLists.txt
    │   ├── configurationtest.cpp
    │   └── main.cpp
    ├── controller
        ├── CMakeLists.txt
        ├── controllertest.cpp
        └── main.cpp

When I want to execute my tests I have to select ControllerTests and Run it. Same thing for ConfigurationTest, individually.

I would like to add something in the top-level CMakeLists.txt of Tests to be able to run all tests in subdirectories. This is my top-level tests CMakeLists.txt

project(MyAppTests)

include_directories(${GTEST_INCLUDE_DIRS})
include_directories($ENV{GMOCK_DIR}/include)

add_subdirectory(config)
add_subdirectory(controller)

Each subdirectories have a main.cpp, and add_executable. In the configuration CMakeLists.txt there is that line :

add_test(NAME ConfigurationTests COMMAND ConfigurationTests)

Upvotes: 1

Views: 1379

Answers (2)

Florian
Florian

Reputation: 42902

Using executable does gives you some more flexibility over using the predefined RUN_TESTS target. I'm using the following call - executed inside my binary output directory - to run all my tests:

ctest.exe -C Debug -j 16 --no-compress-output -T Test --timeout 600

I do want

  • to run the Debug configuration of the tests
  • to run upto 16 tests in parallel
  • to have no-compress-output because I prefer this formatting
  • to Test the software by loading CTestTestfile.cmake and execute the defined tests
    • Also the output and result of each test is recorded (see Testing\...\Test.xml which e.g. can be evaluated by Jenkins xUnit Plugin)
  • to timeout tests after 600 seconds

Upvotes: 1

Blabdouze
Blabdouze

Reputation: 1121

CMake should have automatically generated a target RUN_TESTS that lunch all the tests defined in your project.

If it is not here, you may have forgotten to set BUILD_TESTING option to ON :

CMake will generate tests only if the enable_testing() command has been invoked. The CTest module invokes the command automatically when the BUILD_TESTING option is ON.

Upvotes: 1

Related Questions