CMake Tutorial4_2 测试支持

本文介绍如何使用CMake进行软件测试,包括enable_testing和add_test命令的使用方法。通过实际示例,展示了如何构建并运行测试用例。

一 内容
  1. 请先阅读 CMake Tutorial4_1 安装目标 或更前文章。

  2. 主要说明 如何使用enable_testing和add_test命令测试

  3. 文件目录

    step4_2
    |- MathFunctions
      |- CMakeLists.txt
      |- MathFunctions.h
      |- mysqrt.cc
    |- CMakeaLists.txt
    |- main.cc 
    |- TutorialConfig.h.in
    
  4. 修改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用来验证输出中是否包含指定字符串。
  1. 修改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" 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值