GTest Learning

本文介绍了GoogleTest单元测试库的基本概念及其使用方法。包括单元测试、集成测试与功能测试的区别,gtest断言宏的种类及用途,以及TestCase事件的具体实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Google Test是基于XUnice架构的C++编程语言的单元测试库。

 单元测试

        单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证。对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体含义,如C语言中单元指一个函数,Java里单元指一个类,图形化的软件中可以指一个窗口或一个菜单等。总的来说,单元就是人为规定的最小的被测功能模块。

集成测试

          集成测试,也叫组装测试你或联合测试。在单元测试的基础上,将所有模块按照设计要求(如根据结构图)组装成为子系统或系统,进行集成测试。 实践表明,一些模块虽然能够单独地工作,但并不能保证连接起来也能正常的工作。一些局部反映不出来的问题,在全局上很可能暴露出来。

功能测试

          功能测试就是对产品的各功能进行验证,根据功能测试用例,逐项测试,检查产品是否达到用户要求的功能。

gtest断言

          gtest中提供两种断言的宏:ASSERT宏,EXPECT宏。

          ASSERT宏:当检查点失败时,退出当前函数。   EXPECT宏:当检查点失败时,继续往下执行。

          常用类型检查:

          (1)布尔类型检查   

            ASSERT_TRUE(condition)  ASSERT_FALSE(condition)

            EXPECT_TRUE(condition)  EXPECT_FALSE(condition)

          (2)数值类型检查  

             ASSERT_EQ(val1, val2);             EXPECT_EQ(val1, val2);             ---  equal

             ASSERT_NE(val1, val2);             EXPECT_NE(val1, val2);             ---  not equal

             ASSERT_LT(val1, val2);              EXPECT_LT(val1, val2);               ---  less than

             ASSERT_LE(val1, val2);              EXPECT_LE(val1, val2);              ---  less equal

             ASSERT_GT(val1, val2);              EXPECT_GT(val1, val2);             --- grater than

             ASSERT_GE(val1, val2);              EXPECT_GE(val1, val2);             --- grater equal

             (3)  字符串检查      

             ASSERT_STREQ(expected_str, actual_str);     EXPECT_STREQ(expected_str, actual_str);          --- equal

             ASSERT_STRNE(str1, str2);                                EXPECT_STRNE(str1, str2);                                     --- not equal

             ASSERT_STRCASEEQ(expected_str, actual_str);   EXPECT_STRCASEEQ(expected_str, actual_str);

             ASSERT_STRCASENE(str1, str2);                     EXPECT_STRCASENE(str1, str2);

TestCase事件

            TestCase事件是挂在每个案例执行前后的,不过需要实现的是SetUp方法和TearDown方法:

            1. SetUp()方法在每个TestCase之前执行        2. TearDown()方法在每个TestCase之后执行

单元测试特点

            1. 独立:测试用例的测试结果不受其他测试的影响。

        2.有效的组织架构,清晰的命名:各个测试用例针对不同的测试对象,而对单个测试对象而言,又可能有多个测试用例对应该对象的多个功能。

            3. 可移植、可复用

            4. 当用例失败时,提供尽可能多的有效信息

mymymy@raspberrypi:~/catkin_ws$ catkin_make Base path: /home/mymymy/catkin_ws Source space: /home/mymymy/catkin_ws/src Build space: /home/mymymy/catkin_ws/build Devel space: /home/mymymy/catkin_ws/devel Install space: /home/mymymy/catkin_ws/install #### #### Running command: "make cmake_check_build_system" in "/home/mymymy/catkin_ws/build" #### -- Using CATKIN_DEVEL_PREFIX: /home/mymymy/catkin_ws/devel -- Using CMAKE_PREFIX_PATH: /home/mymymy/catkin_ws/devel;/opt/ros/noetic -- This workspace overlays: /home/mymymy/catkin_ws/devel;/opt/ros/noetic -- Found PythonInterp: /usr/bin/python3 (found suitable version "3.8.10", minimum required is "3") -- Using PYTHON_EXECUTABLE: /usr/bin/python3 -- Using Debian Python package layout -- Using empy: /usr/lib/python3/dist-packages/em.py -- Using CATKIN_ENABLE_TESTING: ON -- Call enable_testing() -- Using CATKIN_TEST_RESULTS_DIR: /home/mymymy/catkin_ws/build/test_results -- Forcing gtest/gmock from source, though one was otherwise available. -- Found gtest sources under '/usr/src/googletest': gtests will be built -- Found gmock sources under '/usr/src/googletest': gmock will be built -- Found PythonInterp: /usr/bin/python3 (found version "3.8.10") -- Using Python nosetests: /usr/bin/nosetests3 -- catkin 0.8.12 -- BUILD_SHARED_LIBS is on -- BUILD_SHARED_LIBS is on -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- ~~ traversing 4 packages in topological order: -- ~~ - learning_communication1 -- ~~ - livox_ros_driver2 -- ~~ - point_lio -- ~~ - learning_tf -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- +++ processing catkin package: 'learning_communication1' -- ==> add_subdirectory(learning_communication1) -- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy -- learning_communication1: 1 messages, 1 services -- +++ processing catkin package: 'livox_ros_driver2' -- ==> add_subdirectory(livox_ros_driver2/livox_ros_driver2) -- livox_ros_driver2 version: 1.2.4 CMake Error at livox_ros_driver2/livox_ros_driver2/CMakeLists.txt:224 (find_package): By not providing "Findament_cmake_auto.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "ament_cmake_auto", but CMake did not find one. Could not find a package configuration file provided by "ament_cmake_auto" with any of the following names: ament_cmake_autoConfig.cmake ament_cmake_auto-config.cmake Add the installation prefix of "ament_cmake_auto" to CMAKE_PREFIX_PATH or set "ament_cmake_auto_DIR" to a directory containing one of the above files. If "ament_cmake_auto" provides a separate development package or SDK, be sure it has been installed. -- Configuring incomplete, errors occurred! See also "/home/mymymy/catkin_ws/build/CMakeFiles/CMakeOutput.log". See also "/home/mymymy/catkin_ws/build/CMakeFiles/CMakeError.log". make: *** [Makefile:1356:cmake_check_build_system] 错误 1 Invoking "make cmake_check_build_system" failed mymymy@raspberrypi:~/catkin_ws$
最新发布
07-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值