• c++使用googletest进行单元测试


    使用Google test进行测试

    使用场景:

    在平时写代码中,我们需要测试某个函数是否正确时可以使用Google test使用,当然,我们也可以自己写函数进行验证,但是使用google test是一个封装好的,使用也很简单,同时显示效果更佳能有效提高我们的效率

    安装方式:

    1. 使用vcpkg: vcpkg install gtest:x64-windows
    2. 官网下载:传送门

    vcpkg(c++包管理器)相关介绍,使用很简单:vcpkg 在clion和vs中的使用

    以下是基于clion和vcpkg使用(vs好像继承了google test):

    这里就不介绍怎么安装了,不会在clion中使用vcpkg安装gtest的可以看一看前面的文章,很快就能看完

    一、单元测试

    CMakeLists.txt文件

    cmake_minimum_required(VERSION 3.28)
    # vcpkg经典模式下建议手动添加这个,不然可能会报错找不到路径,别折腾了
    set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake"
            CACHE STRING "Vcpkg toolchain file")
    
    project(test)
    
    set(CMAKE_CXX_STANDARD 17)
    find_package(GTest CONFIG REQUIRED)
    
    add_executable(test main.cpp)
    
    target_link_libraries(test PRIVATE GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    源代码

    //使用google test进行单元测试
    #include 
    #include 
    
    int add(int a, int b)
    {
    	return a + b;
    }
    
    TEST(testCase1,should_3_when_1_add_2)
    {
    	EXPECT_EQ(3, add(1, 2));
    }
    
    int main(int argc, char **argv)
    {
    	std::cout << "hello world" << std::endl;
        testing::InitGoogleTest(&argc, argv);
    	return RUN_ALL_TESTS();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    测试结果:

    在这里插入图片描述

    解释:

    1. TEST(testCase1,should_3_when_1_add_2) testCase1为测试名称,should_3_when_1_add_2为常用的命名规范,让人们一看就知道要测试什么

    2. EXPECT_EQ(3, add(1, 2)); EXPECT_EQ为测试宏,用于对比函数结果和预期结果,如果结果相同则测试正确,不同则报错

      这种是固定写法,用于测试使用

    3.  int main(int argc, char **argv)
       {
           testing::InitGoogleTest(&argc, argv);
           return RUN_ALL_TESTS();
       }
      
      • 1
      • 2
      • 3
      • 4
      • 5

    错误示例:

    在这里插入图片描述

    二、使用gmock测试

    gmock测试呢,就是在你将函数写出来了,但还没有实现定义,这是你可以使用gmock实现简单的测试。

    使用gmock时需要**继承**要gmock的类,例如我需要gmock User类的get_name函数,可以这样使用

    //User类定义
    #ifndef USER_H
    #define USER_H
    #include 
    class User{
    public:
      virtual std::string get_name() = 0;
    };
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    //MockUser类定义
    #ifndef MOCKUSER_H
    #define MOCKUSER_H
    
    #include "User.h"
    #include 
    class MockUser: public User{
    public:
      MOCK_METHOD(std::string, get_name, (), (override));
    };
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    //使用google test进行单元测试
    #include 
    #include 
    #include "MockUser.h"
    int add(int a, int b) {
    	return a + b;
    }
    
    TEST(testCase1, should_3_when_1_add_2) {
    	EXPECT_EQ(3, add(1, 2));
    }
    
    TEST(testCase2, test) {
    	MockUser user;
    	EXPECT_CALL(user, get_name()).Times(testing::AtLeast(3)).
    	WillOnce(testing::Return("one")).
    	WillOnce(testing::Return("two")).
    	WillRepeatedly(testing::Return("other"));
    
    	std::cout << user.get_name() << std::endl;
    	std::cout << user.get_name() << std::endl;
    	std::cout << user.get_name() << std::endl;
    	std::cout << user.get_name() << std::endl;
    	std::cout << user.get_name() << std::endl;
    	std::cout << user.get_name() << std::endl;
    }
    
    int main(int argc, char **argv) {
    	std::cout << "hello world" << std::endl;
    	testing::InitGoogleTest(&argc, argv);
    	return RUN_ALL_TESTS();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    测试结果:

    在这里插入图片描述

    解释:

    1. Times(testing::AtLeast(3))设置至少执行三次,如果少于三次就错误
    2. WillOnce(testing::Return(“one”)). WillOnce表示执行一次,Return表示传入的参数,这里有两个WillOnce分别调式第一次调用和第二次调用分别传入one和two
    3. WillRepeatedly(testing::Return(“other”)); 表示递归调用,这里的Return表示后续递归调用传入的值,前两次输出one和two,后面输出other
  • 相关阅读:
    深入 Hyperf:HTTP 服务启动时发生了什么?
    【鸿蒙软件开发】ArkTS基础组件之Marquee(文字跑马灯)、QRCode(二维码生成)
    LeetCode:771.宝石与石头
    刷题记录(NC50959 To the Max,NC236172 货船,NC16655 [NOIP2005]过河)
    小白量化《穿云箭集群量化》(1)小白草根超级量化软件介绍
    MyBatis中文网
    基于观察者模式设计的框架-REB,使代码模块化
    基于Android+OpenCV+CNN+Keras的智能手语数字实时翻译——深度学习算法应用(含Python、ipynb工程源码)+数据集(四)
    2024-5-9-从0到1手写配置中心Config之@ConfigurationProperties热更新
    MySQL定位CPU利用率过高的SQL方法
  • 原文地址:https://blog.csdn.net/weixin_73186358/article/details/137974697