51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1132|回复: 0
打印 上一主题 下一主题

[转贴] CMake实战:安装测试和添加环境生成安装包(上)

[复制链接]
  • TA的每日心情
    擦汗
    2 小时前
  • 签到天数: 1026 天

    连续签到: 1 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2022-1-14 13:30:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    1、安装测试
      CMake 也可以指定安装规则,以及添加测试。这两个功能分别可以通过在产生 Makefile 后使用 make install 和 make test 来执行。在 GNU Makefile 里,你可能需要为此编写 install 和 test 两个伪目标和相应的规则,但在 CMake 里,这样的工作同样只需要简单的调用几条命令。
      1.1定制安装规则
      首先先在 math/CMakeLists.txt 文件里添加下面两行:
    1. # 指定 MathFunctions 库的安装路径
    2.   install (TARGETS MathFunctions DESTINATION bin)
    3.   install (FILES MathFunctions.h DESTINATION include)
    复制代码
    指明 MathFunctions 库的安装路径。之后同样修改根目录的 CMakeLists 文件,在末尾添加下面几行:

    1. # 指定安装路径
    2.   install (TARGETS Demo DESTINATION bin)
    3.   install (FILES "${PROJECT_BINARY_DIR}/config.h"
    4.            DESTINATION include)
    复制代码
     通过上面的定制,生成的 Demo 文件和 MathFunctions 函数库 libMathFunctions.o 文件将会被复制到 /usr/local/bin 中,而 MathFunctions.h 和生成的 config.h 文件则会被复制到 /usr/local/include 中。我们可以验证一下(顺带一提的是,这里的 /usr/local/ 是默认安装到的根目录,可以通过修改 CMAKE_INSTALL_PREFIX 变量的值来指定这些文件应该拷贝到哪个根目录):

    1. [root@hackett demo5]# make install
    2.   Consolidate compiler generated dependencies of target MathFunctions
    3.   [ 50%] Built target MathFunctions
    4.   Consolidate compiler generated dependencies of target demo
    5.   [100%] Built target demo
    6.   Install the project...
    7.   -- Install configuration: ""
    8.   -- Installing: /usr/local/bin/demo
    9.   -- Installing: /usr/local/include/config.h
    10.   -- Installing: /usr/local/bin/libMathFunctions.a
    11.   -- Installing: /usr/local/include/myMath.h
    12.   [root@hackett demo5]# ls /usr/local/bin/
    13.   demo      libMathFunctions.a               
    14.   [root@iZwz97bu0gr8vx0j8l6kkzZ demo5]# ls /usr/local/include/
    15.   config.h myMath.h
    复制代码
    1.2工程添加测试
      添加测试同样很简单。CMake 提供了一个称为 CTest 的测试工具。我们要做的只是在项目根目录的 CMakeLists 文件中调用一系列的 add_test 命令。
      CMakeLists.txt
    1. cmake_minimum_required(VERSION 3.10)
    2.   # set the project name
    3.   project(demo5)
    4.   # 加入一个配置头文件,用于处理 CMake 对源码的设置
    5.   configure_file (
    6.       "${PROJECT_SOURCE_DIR}/config.h.in"
    7.       "${PROJECT_BINARY_DIR}/config.h"
    8.   )
    9.   # 是否使用自己的 MathFunctions 库
    10.   option (USE_MYMATH
    11.           "Use provided math implementation" ON)
    12.   # 是否加入 MathFunctions 库
    13.   if (USE_MYMATH)
    14.       include_directories ("${PROJECT_SOURCE_DIR}/math")
    15.       add_subdirectory (math)
    16.       set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
    17.   endif (USE_MYMATH)
    18.   aux_source_directory(. DIR_SRCS)
    19.   # 指定生成目标
    20.   add_executable(demo ${DIR_SRCS})
    21.   target_link_libraries(demo ${EXTRA_LIBS})
    22.   # 指定安装路径
    23.   install (TARGETS demo DESTINATION bin)
    24.   install (FILES "${PROJECT_BINARY_DIR}/config.h"
    25.                    DESTINATION include)
    26.   enable_testing()
    27.   # 测试程序是否成功运行
    28.   add_test (test_run demo 3 2)
    29.   add_test (test_35_2 demo 35 2)
    30.   set_tests_properties (test_35_2 PROPERTIES PASS_REGULAR_EXPRESSION "37")
    31.   add_test (test_5_2 demo 5 2)
    32.   set_tests_properties (test_5_2 PROPERTIES PASS_REGULAR_EXPRESSION "7")
    33.   add_test (test_2_3 demo 2 3)
    34.   set_tests_properties (test_2_3 PROPERTIES PASS_REGULAR_EXPRESSION "5")
    复制代码
    上面的代码包含了四个测试。第一个测试 test_run 用来测试程序是否成功运行并返回 0 值。剩下的三个测试分别用来测试 35 + 2 、5 + 2、2 + 3是否都能得到正确的结果。其中 PASS_REGULAR_EXPRESSION 用来测试输出是否包含后面跟着的字符串。
      测试结果:
    1. [root@hackett demo5]# make
    2.   Consolidate compiler generated dependencies of target MathFunctions
    3.   [ 50%] Built target MathFunctions
    4.   Consolidate compiler generated dependencies of target demo
    5.   [ 75%] Building CXX object CMakeFiles/demo.dir/main.cpp.o
    6.   [100%] Linking CXX executable demo
    7.   [100%] Built target demo
    8.   [root@hackett demo5]# make test
    9.   Running tests...
    10.   Test project /root/workspace/cmake/demo5
    11.       Start 1: test_run
    12.   1/4 Test #1: test_run .........................   Passed    0.00 sec
    13.       Start 2: test_35_2
    14.   2/4 Test #2: test_35_2 ........................   Passed    0.00 sec
    15.       Start 3: test_5_2
    16.   3/4 Test #3: test_5_2 .........................   Passed    0.00 sec
    17.       Start 4: test_2_3
    18.   4/4 Test #4: test_2_3 .........................   Passed    0.00 sec
    19.   100% tests passed, 0 tests failed out of 4
    20.   Total Test time (real) =   0.01 sec
    复制代码
    如果要测试更多的输入数据,像上面那样一个个写测试用例未免太繁琐。这时可以通过编写宏来实现:
    1.  # 定义一个宏,用来简化测试工作
    2.   macro (do_test arg1 arg2 result)
    3.     add_test (test_${arg1}_${arg2} demo ${arg1} ${arg2})
    4.     set_tests_properties (test_${arg1}_${arg2}
    5.       PROPERTIES PASS_REGULAR_EXPRESSION ${result})
    6.   endmacro (do_test)
    7.   
    8.   # 使用该宏进行一系列的数据测试
    9.   do_test (35 2 "37")
    10.   do_test (5 52 "7")
    11.   do_test (2 3 "5")
    复制代码
     关于 CTest 的更详细的用法可以通过 man 1 ctest 参考 CTest 的文档。



    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-9-29 11:33 , Processed in 0.088729 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表