CMake-官方教程(5)-安装文件与添加测试用例-Installing and Testing

Installing and Testing

Installing

使用CMakeLists.txt安装二进制文件、库、头文件:

set(installable_libs MathFunctions tutorial_compiler_flags)
install(TARGETS ${installable_libs} DESTINATION lib)

install(TARGETS Tutorial DESTINATION bin)

安装指定目标到lib文件夹中

install(FILES MathFunctions.h DESTINATION include)
install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  DESTINATION include
  )

安装文件到指定目录中

安装指令:

cmake --install . --config Debug --prefix "/home/myuser/installdir"

安装Debug版本相关文件,安装目录的前缀是/home/myuser/installdir

Testing

为可执行文件添加测试。
开启测试

enable_testing()
add_test(NAME Runs COMMAND Tutorial 25)

添加名为Runs的测试,指令为Tutorial 25

add_test(NAME StandardUse COMMAND Tutorial 4)
set_tests_properties(StandardUse
  PROPERTIES PASS_REGULAR_EXPRESSION "4 is 2"
  )

添加测试,并验证正则是否匹配输出

function(do_test target arg result)
  add_test(NAME Comp${arg} COMMAND ${target} ${arg})
  set_tests_properties(Comp${arg}
    PROPERTIES PASS_REGULAR_EXPRESSION ${result}
    )
endfunction()

# do a bunch of result based tests
do_test(Tutorial 4 "4 is 2")
do_test(Tutorial 9 "9 is 3")
do_test(Tutorial 5 "5 is 2.236")
do_test(Tutorial 7 "7 is 2.645")
do_test(Tutorial 25 "25 is 5")
do_test(Tutorial -25 "-25 is (-nan|nan|0)")
do_test(Tutorial 0.0001 "0.0001 is 0.01")

定义函数do_test方便添加测试用例

执行测试用例

ctest -C Debug

指定Debug版本进行测试


版权声明:本文为mrbone11原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。