首页 > 编程语言 >开发之单元测试—Test_C++的gtest单元测试

开发之单元测试—Test_C++的gtest单元测试

时间:2024-03-20 15:47:46浏览次数:20  
标签:单元测试 TEST gtest EXPECT 测试 Test main

单元测试

测试的本质其实都是一样的,都是通过给定参数来执行函数,然后判断函数的实际输出结果和期望输出结果是否一致

测试框架

gtest采用的是xUnit架构,
JUnit
PyUnit: PyUnit 主要用于进行白盒测试和回归测试

C++测试

 gtest单元测试是Google的一套用于编写C++测试的框架,
  可以运行在很多平台上(包括Linux、Mac OS X、Windows、Cygwin等等)。基于xUnit架构
 
基本了解
  gtest系列之TEST宏
   TEST宏的作用是创建一个简单测试,它定义了一个测试函数,在这个函数里可以使用任何C++代码并使用提供的断言来进行检查
   #define TEST(test_suite_name, test_name) GTEST_TEST(test_suite_name, test_name)
    #endif
  二、gtest系列之断言 
    gtest中断言的宏可以分为两类:一类是ASSERT宏,另一类就是EXPECT宏了。 
    1、ASSERT_系列:如果当前点检测失败则退出当前函数 
    2、EXPECT_系列:如果当前点检测失败则继续往下执行 
 三:
    gtest的事件一共有三种: 
      1、TestSuite事件
      2、TestCase事件 
      3.全局事件 		  
 全局事件
   来做一个测试实例,测试Map的find()和size()函数并通过继承testing::Test来实现。 
   此时的TEST宏必须写成TEST_F,而且它的第一个参数必须与类名一致
 
1.安装:
   lib目录下会生成:libgmock.a libgmock_main.a libgtest.a libgtest_main.a
2.使用
   #include "gtest/gtest.h"
   
    01.写main方法,其中调用RUN_ALL_TESTS函数即可。
     int main(int argc, char **argv) {
      testing::InitGoogleTest(&argc, argv);
      return RUN_ALL_TESTS();   
     }
    
    02、我们也可以不用写main函数,那就需要链接 gtest_main.a这个静态库
         	//测试哪个函数以及这个测试的名字(
         TEST(FactorialTest, Negative) {
             // This test is named "Negative", and belongs to the "FactorialTest"
             // test case.
             // 断言,运行Factorial(-5)并对比结果是不是等于1(Factorial(-5)==1?)
             EXPECT_EQ(1, Factorial(-5));
             EXPECT_EQ(1, Factorial(-1));
             // 断言,运行Factorial(-10)并对比结果是不是小于0(0 > actorial(-10)?)
             EXPECT_GT(Factorial(-10), 0);
         }
3编译运行
 编译呢
   1、g++ sample1.cc sample1_unittest.cc -lgtest -std=c++14 -lgtest_main -lpthread -o test1
   CMakeLists.txt
 运行 

参考

  https://blog.csdn.net/Jacksqh/article/details/129805462  
     gtest断言类型
     ASSERT_*断言和EXPECT_*断言的区别
 https://github.com/google/googletest/blob/main/googletest/include/gtest/gtest.h

标签:单元测试,TEST,gtest,EXPECT,测试,Test,main
From: https://www.cnblogs.com/ytwang/p/18085368

相关文章

  • JB Loves Math(The 19th Zhejiang Provincial Collegiate Programming Contest)
    #include<bits/stdc++.h>#defineendl'\n'usingll=longlong;typedefunsignedlonglongull;usingnamespacestd;voidGordenGhost();signedmain(){#ifdefGordenfreopen("in.txt","rt",stdin);freopen......
  • JB Wants to Earn Big Money(The 19th Zhejiang Provincial Collegiate Programming Co
    #include<bits/stdc++.h>#defineendl'\n'usingll=longlong;typedefunsignedlonglongull;usingnamespacestd;voidGordenGhost();signedmain(){#ifdefGordenfreopen("in.txt","rt",stdin);freopen......
  • JB Loves Comma(The 19th Zhejiang Provincial Collegiate Programming Contest)
    #include<bits/stdc++.h>#defineendl'\n'usingll=longlong;typedefunsignedlonglongull;usingnamespacestd;voidGordenGhost();signedmain(){#ifdefGordenfreopen("in.txt","rt",stdin);freopen......
  • AtCoder Beginner Contest 345
    A-Leftrightarrow#include<bits/stdc++.h>usingnamespacestd;usingi32=int32_t;usingi64=longlong;usingldb=longdouble;#defineintlonglongusingvi=vector<int>;usingpii=pair<int,int>;constintmod=1e9+7;......
  • Pytest教程:详解pytest.main()
    1.什么是 pytest.main() 函数?pytest.main()是Pytest测试框架中的一个函数,用于动态地运行测试。通过调用这个函数,可以在代码中以编程方式执行Pytest测试集而不是通过命令行。1.1 pytest.main() 函数的作用和功能:pytest.main() 函数的主要作用是启动Pytest测试......
  • AtCoder Beginner Contest 345 A-F 题解
    A-LeftrightarrowQuestion给你一个由<、=和>组成的字符串\(S\)。请判断\(S\)是否是双向箭头字符串。字符串\(S\)是双向箭头字符串,当且仅当存在一个正整数\(k\),使得\(S\)是一个<、\(k\)个=和一个>的连接,且顺序如此,长度为\((k+2)\)。Solution按照题意......
  • Monitor test Philips 279P1B 4K 60FPS 10bit HDR400 99%-Adobe RGB, 99% P3, 99% SRG
    SoIboughtthismonitor.Thespecsarefromhere: https://www.usa.philips.com/c-p/279P1B_27/brilliance-lcd-monitor-with-usb-c ---wordsinshort,DP1.4,HDMI2.0,DP1.4viaUSB-CPD90WmaxWhenenablingUSB-Chigh-resolutionmode(10bit),withmym1......
  • pytest+allure生成html报告(入门篇)
    一. 安装pytest库pipinstallpytest代码如下(示例):importpytestclassTestLogin:deftest_login(self):print('---------------login--------------')if__name__=='__main__':pytest.main(['-vs']) 运行结果如下:二、在pyth......
  • python pytest.mark
    pytest.mark是pytest测试框架中的一个功能,它允许你为测试用例和测试夹具(fixtures)添加元数据(或称为“标记”)。这些标记可以用于各种目的,例如选择性地运行特定的测试用例集,跳过某些测试用例,或者为测试用例添加额外的信息。pytest.mark主要有以下几种用法:为测试用例添加标记:你......
  • 如何调用Pytest
    如何调用Pytest在模块中运行测试pytesttest_mod.py在目录中运行测试pytesttesting/通过关键字表达式运行测试pytest-k'MyClassandnotmethod'这将运行包含与给定字符串表达式(不区分大小写)匹配的名称的测试,其中可以包括使用文件名、类名和函数名作为变量的Python......