GTEST学习总结

目录

1.编译及学习资料

1.1编译gtest

1.2学习文档及资料

2.gtest总结       

2.1gtest中的术语

2.2断言

2.2.1基本断言

2.2.2Binary Comparison

2.2.3String comparison

2.3创建测试用例

2.4TestFixtures

2.5更多断言方法

2.6异常断言

2.7自定义输出语句

2.8谓词断言 pred

2.9AssertionResult

2.10Predicate-Formatter

2.11浮点数比较

2.12自定义设置可接受的范围

2.13gMock Matchers

2.14类型断言(type assertions)

2.15Death Test


1.编译及学习资料


1.1编译gtest


https://blog.csdn.net/lm409/article/details/55049893
https://blog.csdn.net/liyuefeilong/article/details/50993607
https://blog.csdn.net/qq_26437925/article/details/56479841

1.2学习文档及资料

gtestd.lib/gtest.lib 与 gtest_maind.lib/gtest_main.lib的区别?
https://stackoverflow.com/questions/6457856/whats-the-difference-between-gtest-lib-and-gtest-main-lib

https://wenku.baidu.com/view/abec0f8f700abb68a882fb67.html
https://blog.csdn.net/lywzgzl/article/details/52203558

文档
https://github.com/google/googletest/blob/master/googletest/docs/primer.md

primer入门书
https://github.com/google/googletest/blob/master/googletest/docs/primer.md

2.gtest总结       

2.1gtest中的术语


TEST     对应于平时理解的 TESTCASE
TESTCASE 对应于平时理解的 TESTSUITE  

2.2断言


ASSERT_*   在失败时会生成致命故障,并中止当前的功能
EXPECT_*   生成非致命故障,不会中止当前故障
通常情况下,使用EXPECT_*,因为它们允许在测试中报告多个失败;只有,当出现某个失败后,接下去的测试都无意义时,则使用ASSERT_*


2.2.1基本断言


Fatal assertion                            Nonfatal assertion                    Verifies
ASSERT_TRUE(condition)        EXPECT_TRUE(condition)     condition is true
ASSERT_FALSE(condition)      EXPECT_FALSE(condition)    condition is false

2.2.2Binary Comparison


 Fatal assertion                      Nonfatal assertion                  Verifies
ASSERT_EQ(val1,val2)        EXPECT_EQ(val1,val2)        val1 == val2
ASSERT_NE(val1,val2)        EXPECT_NE(val1,val2)        val1 != val2
ASSERT_LT(val1,val2)        EXPECT_LT(val1,val2)        val1 < val2
ASSERT_LE(val1,val2)        EXPECT_LE(val1,val2)        val1 <= val2
ASSERT_GT(val1,val2)        EXPECT_GT(val1,val2)        val1 > val2
ASSERT_GE(val1,val2)        EXPECT_GE(val1,val2)        val1 >= val2


EQ: EQUAL
NE: NOT EQUAL
LT: LESS THAN
LE: LESS EQUAL
GT: GREAT THAN
GE: GREAT EQUAL

判断是否是空指针时,使用nullptr,如*_EQ(ptr,nullptr)

 

2.2.3String comparison


比较两个字符串,如果要比较两个字符对象,则使用 binary comparison
Fatal assertion            Nonfatal assertion        Verifies
ASSERT_STREQ(str1,str2)        EXPECT_STREQ(str1,str2)        the two C strings have the same content
ASSERT_STRNE(str1,str2)        EXPECT_STRNE(str1,str2)            the two C strings have the different content
ASSERT_STRCASEEQ(str1,str2)    EXPECT_STRCASEEQ(str1,str2)    the two C strings have the same content,ignoring case
ASSERT_STRCASENE(str1,str2)    EXPECT_STRCASENE(str1,str2)    the two C strings have different contents, ignoring case

注意,断言方法名称中的"CASE"表示忽略大小写的意思。一个空指针NULL 与 空字符串是不相等的。

2.3创建测试用例

 

创建一个测试用例(TEST):
1.使用锚 TEST()
2.函数体支持C++语句
3.测试结果的成功与否与断言语句有关。只要有一条断言失败,可者是测试崩了,那么这个测试用例就算是失败的。

TEST(TestCaseName,TestName){
    ...testBody...
}

注意:这里的TestCaseName,即TestSuiteName
而 TestName,即TestCaseName

2.4TestFixtures


创建测试集:多个测试用例,使用相同的配置和数据
创建方法:
1. 从 ::testing::Test 派生一个类。使用protected 启动它的主体,因为我们需要从子类访问fixture的成员
2.在类中,声明需要使用的对象
3.如有必要,写一个默认的构造函数 或 SetUp() 方法 用来 为每个测试准备对象。注意:SetUp()中的U为大写
4.如有必要,写一个析构函数 或 TearDown()方法 用来 释放在SetUp()中分配的任何资源
5.如有需要,为要共享的测试定义子例程。


使用 fixture时,需要使用TEST_F()

TEST_F(TestCaseName,TestName){
    ..testBody..
}

这里的TestCaseName, 要与第1.中的 fixture 名称一致。
每执行一个TEST_F(),会执行一次fixture中的SetUp() 和 TearDown();

执行测试:
在main函数中调用RUN_ALL_TESTS()

main函数写法如下:
int main (int argc, char **argv){
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

若不想自己写main()函数,可以Link gtest_main.lib/gtest_maind.lib

gtest进阶(Advanced googletest Topics)
https://github.com/google/googletest/blob/master/googletest/docs/advanced.md

2.5更多断言方法


返回型:
SUCCEED();
FAIL();
ADD_FAILURE();
ADD_FAILURE_AT();

注:各种断言都可自定定义输出,但只有当执行失败时,才会输出自定义的语句。


2.6异常断言


用户测试程序中的断言抛出是否正确
Fatal assertion                    Nonfatal assertion                Verifies
ASSERT_THROW(statement,exception_type)        EXPECT_THROW(statement,exception_type)        statement throws an exception of the given type
ASSERT_ANY_THROW(statement)            EXPECT_ANY_THROW(statement)            statement throws an exception of any type
ASSERT_NO_THROW(statement)            EXPECT_NO_THROW(statement)            statement doesn't throw any exception

注:这些断言宏都接受C/C++语句作为参数,所以既可以像前面那样传递表达式,也可以传递用大括号包起来的语句块


2.7自定义输出语句

2.8谓词断言 pred

Fatal assertion                    Nonfatal assertion                Verifies
ASSERT_PRED1(pred1,val1)            EXPECT_PRED1(pred1,val1)            pred1(val1) is true
ASSERT_PRED2(pred2,val1,val2)            EXPECT_PRED2(pred2,val1,val2)            pred2(val1,val2) is true
...                        ...                        ...

注:predn 中,n 最大为 5,即最多支持5个参数的谓词断言;注意,当谓词函数存在重载时,需要指定调用的哪个类型的函数,否则会出错;
另外,在使用到模板谓词函数时,存在多个参数时,需要在调用时将模板函数括起来,否则,也会出错。


2.9AssertionResult


断言结果,用来自定义失败时显示的结果

2.10Predicate-Formatter


Fatal assertion                    Nonfatal assertion                Verifies
ASSERT_PRED_FORMAT1(pred_format1,val1)        EXPECT_PRED_FORMAT1(pred_format1,val1)        pred_format1(val1) is successful
ASSERT_PRED_FORMAT2(prd_format2, val1,val2)    EXPECT_PRED_FORMAT2(pred_format2,val1,val2)    pred_format2(val1,val2) is successful
...


2.11浮点数比较


Fatal assertion                    Nonfatal assertion                Verifies
ASSERT_FLOAT_EQ(val1,val2)            EXPECT_FLOAT_EQ(val1,val2)            the two float values are almost equal
ASSERT_DOUBLE_EQ(val1,val2)            EXPECT_DOUBLE_EQ(val1,val2)            the two double values are almost equal

注:"almost equal"即 在 4 ULP 之内

2.12自定义设置可接受的范围


ASSERT_NEAR(val1,val2,abs_error)        EXPECT_NEAR(val1,val2,abs_error)        the difference between val1 and val2 doesn't exceed the given absolute error


2.13gMock Matchers


ASSERT_THAT(value,matcher)            EXPECT_THAT(value,matcher)            value matches matcher
    
use gMock string matchers with EXPECT_THAT() or ASSERT_THAT() to do more string comparison tricks
such as substring, prefix, suffix, regular expression and etc.

2.14类型断言(type assertions)


::testing::StaticAssertTypeEq<T1,T2>()
注意:
类模板或函数模板的成员函数中使用时,StaticAssertTypeEq <T1,T2>()仅在实例化函数时有效。

2.15Death Test


ASSERT_DEATH(statement,regex)            EXPECT_DEATH(statement,regex)            statement crashes with the given error
ASSERT_DEATH_IF_SUPPORTED(statement,regex)    EXPECT_DEATH_IF_SUPPORTED(statement,regex)    if death tests are supported, verifies that statement crashes with the given error; otherwise verifies nothing
ASSERT_EXIT(statement,predicate,regex)                                                                                                                                                                                     

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值