一 内容
-
请先阅读 CMake Tutorial4_1 安装目标 或更前文章。
-
主要说明 如何使用enable_testing和add_test命令测试。
-
文件目录
step4_2 |- MathFunctions |- CMakeLists.txt |- MathFunctions.h |- mysqrt.cc |- CMakeaLists.txt |- main.cc |- TutorialConfig.h.in -
修改CMakeLists.txt
# 设置CMake最低版本 cmake_minimum_required(VERSION 3.16) # 设置项目名称及版本 project(Tutorial VERSION 1.0) # 生成可执行文件 add_executable(Tutorial main.cc) # 设置选项 option(USE_MYMATH "use Turotial math" ON) # 配置文件:拷贝文件到另一位置,并且修改其内容 configure_file(TutorialConfig.h.in TutorialConfig.h) if(USE_MYMATH) add_subdirectory(MathFunctions) list(APPEND extra_libs MathFunctions) endif() target_link_libraries(Tutorial PUBLIC ${ extra_libs}) # 打印信息 message("PROJECT_BINARY_DIR=${PROJECT_BINARY_DIR}") # 增加include路径到目标,否则会无法include生成的TutorialConfig.h target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}") install(TARGETS Tutorial DESTINATION bin) install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" DESTINATION include) # ----add enable_testing() # does the application run add_test(NAME Runs COMMAND Tutorial 25) # does the usage message work? add_test(NAME Usage COMMAND Tutorial) set_tests_properties(Usage PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number" ) # define a function to simplify adding tests 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_test) # 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")
- 测试属性PASS_REGULAR_EXPRESSION用来验证输出中是否包含指定字符串。
-
修改main.cc
// 使用官方例子 // A simple program that computes the square root of a number #include <cmath> #include <iostream> #include <string> #include "TutorialConfig.h" // should we include the MathFunctions header? #ifdef USE_MYMATH # include "MathFunctions.h" #endif int main(int argc, char* argv[]) { if (argc < 2) { // report version std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "." << Tutorial_VERSION_MINOR << std::endl; std::cout << "Usage: " << argv[0] << " number"

本文介绍如何使用CMake进行软件测试,包括enable_testing和add_test命令的使用方法。通过实际示例,展示了如何构建并运行测试用例。
最低0.47元/天 解锁文章
1314

被折叠的 条评论
为什么被折叠?



