CMake exclude tests in subdirectories

陌路散爱 提交于 2019-12-08 17:11:56

问题


I have a c++ project which includes libevent library. Project structure:

.
|_ CMakeLists.txt
|_ Makefile
|_ src
| |_ my_lib.cpp
|_ test
| |_ my_lib_test.cpp
|_ lib
  |_ libevent
    |_ CMakeLists.txt
    |_ ...

When I build and run my tests, libevent tests are also executed. How can I exclude them and run only my own tests?


回答1:


Looking at the available options in libevent's CMakeLists.txt file, it appears that you can disable these pretty easily by setting EVENT__DISABLE_TESTS to ON.

You can either do this in your own CMakeLists.txt before libevent is included:

set(EVENT__DISABLE_TESTS ON)
...
add_subdirectory(lib/libevent)

or when you invoke CMake on the command line:

cmake . -DEVENT__DISABLE_TESTS=ON



回答2:


There is also a more general way to do it. Add a file named CTestCustom.cmake to your source tree and add a list of tests that you want CTest not to run:

set(CTEST_CUSTOM_TESTS_IGNORE
   test1
   ....
   testN
)

Than copy this file to the build directory where tests are executed:

configure_file(${CMAKE_SOURCE_DIR}/CTestCustom.cmake ${CMAKE_BINARY_DIR})

This will make CTest ignore the listed tests. See this for more info.




回答3:


I agree with @Ivan Baidakou.

I'm not sure this is much better, but here's a non-portable hack (won't work on Windows by default) to get it done.

execute_process(COMMAND sh -c "echo 'set(CTEST_CUSTOM_TESTS_IGNORE'; find '${CMAKE_SOURCE_DIR}/relative_paths' -path '*tests/CMakeLists.txt' -exec sed -nE '/add_test.*NAME/{s|.*NAME *([^ ]+).*|\\1|; p}' {} +; echo ')'"
                OUTPUT_FILE ${CMAKE_BINARY_DIR}/CTestCustom.cmake)

Where ${CMAKE_SOURCE_DIR}/relative_paths can be multiple paths for multiple libraries if you want.



来源:https://stackoverflow.com/questions/23713650/cmake-exclude-tests-in-subdirectories

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!