如何添加CMake自定义目标来测试所有子项目?

时间:2015-07-26 13:09:12

标签: cmake ctest

我有一个包含多个项目的存储库。生成的目录结构如下:

Repository
  --CMakeLists.txt
  --Project A
      --CMakeLists.txt
  --Project B
      --CMakeLists.txt

在项目A和B中,我有使用add_test添加的测试。在项目内部,我可以make test

如何添加目标"测试"到顶部CMakeLists.txt能够从顶层目录调用make test并获得所有项目执行的测试?

1 个答案:

答案 0 :(得分:4)

我刚刚尝试使用以下文件重现您的设置:

/tmp/test $ tree
.
├── a
│   └── CMakeLists.txt
├── b
│   └── CMakeLists.txt
└── CMakeLists.txt

/tmp/test $ cat CMakeLists.txt 
cmake_minimum_required(VERSION 3.0)

project(Foo)

enable_testing()
add_subdirectory(a)
add_subdirectory(b)
/tmp/test $ cat a/CMakeLists.txt 
project(A)
add_test(NAME atest COMMAND echo "hello from a")
/tmp/test $ cat b/CMakeLists.txt 
project(B)
add_test(NAME btest COMMAND echo "hello from b")

/tmp/test/build $ mkdir build && cd build
/tmp/test/build $ cmake .. && make test
# Remove some output
Running tests...
Test project /tmp/test/build
    Start 1: atest
1/2 Test #1: atest ............................   Passed    0.00 sec
    Start 2: btest
2/2 Test #2: btest ............................   Passed    0.00 sec