【发布时间】:2015-04-05 11:49:32
【问题描述】:
我想用 CMake 编写一个小单元测试系统,我可以轻松地复制和粘贴到我的所有项目中,所以我想出了这个:
include(CMakeParseArguments)
set(UNIT_TEST "unit_tests")
add_custom_target(${UNIT_TEST} ALL VERBATIM)
function(add_unit_test dependency)
cmake_parse_arguments(UT_ "" "NAME" "" ${ARGN})
if(NOT ${UT_NAME})
set(${UT_NAME} ${ARG0})
endif()
add_test(${ARGN})
add_dependencies(${UNIT_TEST} ${dependency})
add_custom_command(TARGET ${UNIT_TEST}
COMMENT "Run tests"
POST_BUILD COMMAND ctest
ARGS -R ${UT_NAME} --output-on-failures
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
VERBATIM)
endfunction(add_unit_test)
我希望它能够像这样正常工作并运行我将通过调用 add_unit_test(dep ...) 来添加到我的项目中的所有单元测试,并在之前编译依赖项,然后使用与 add_test(...) 相同的参数。实际上会出现此错误:
CMake Warning (dev) at cmake/testing.cmake:13 (add_custom_command):
Policy CMP0040 is not set: The target in the TARGET signature of
add_custom_command() must exist. Run "cmake --help-policy CMP0040" for
policy details. Use the cmake_policy command to set the policy and
suppress this warning.
The target name "unit_tests" is unknown in this context.
Call Stack (most recent call first):
source/test/CMakeLists.txt:10 (add_unit_test)
This warning is for project developers. Use -Wno-dev to suppress it.
为什么此时目标是未知的? include(cmake/testing.cmake) 是我在项目构建脚本中在cmake_minimum_required 之后调用的第一件事,所以不可能是因为尚未调用add_custom_target(${UNIT_TEST} ALL VERBATIM)。
有没有办法可以将自定义命令添加到 UNIT_TEST 目标?
【问题讨论】:
标签: unit-testing cmake target