C语言单元测试专家指南(gtest)(一)

为了便于初学者理解,C语言单元测试的方法,写了实例教程.
初学者1个星期就能完成学习,理解并运用.


1. EXPECT_TRUE

基本判定 使用场景: 验证结果为真
对真/假的条件进行判定

终止断言普通判定验证
ASSERT_TRUE(条件);EXPECT_TRUE(条件);验证条件为真
ASSERT_FALSE(条件);EXPECT_FALSE(条件);验证条件为假

测试举例

 运行结果

[ RUN      ] HelloGtest.case_EXPECT_TRUE
Hello Gtest!
[       OK ] HelloGtest.case_EXPECT_TRUE (0 ms)
 命中[ 2 ]总行数[ 2 ]覆盖率[100.0%] 

 2. ASSERT_TRUE

ASSERT_TRUE, 使用场景: 验证结果为真,失败时,程序终止
对真/假的条件进行判定

终止断言普通判定验证
ASSERT_TRUE(条件);EXPECT_TRUE(条件);验证条件为真
ASSERT_FALSE(条件);EXPECT_FALSE(条件);验证条件为假

测试举例

运行结果
 

[ RUN      ] HelloGtest.case_ASSERT_TRUE
case_ASSERT_TRUE.c:10: Failure
Value of: value==32768
  Actual: false
Expected: true
32767+1 is not 32768, which is-32768
[  FAILED  ] HelloGtest.case_ASSERT_TRUE (0 ms)
 命中[ 2 ]总行数[ 2 ]覆盖率[100.0%] 

 3.EXPECT_EQ

EXPECT_EQ 数值比较判定 使用场景: 确认两个数值相等

对数值条件进行比较判定

终止断言普通判定验证
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

测试举例

运行结果

[ RUN      ] HelloGtest.case_EXPECT_EQ
[       OK ] HelloGtest.case_EXPECT_EQ (0 ms)
 命中[ 4 ]总行数[ 8 ]覆盖率[50.0%] 

 EXPECT_STREQ

字符串比较 使用场景: 字符串比较

Fatal assertionNonfatal assertionVerifies
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 different contents
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

 代码举例

 运行结果

[ RUN      ] HelloGtest.case_EXPECT_STREQ
[       OK ] HelloGtest.case_EXPECT_STREQ (0 ms)
 命中[ 3 ]总行数[ 10 ]覆盖率[30.0%] 

EXPECT_FLOAT_EQ

浮点数比较 使用场景: 浮点数比较

浮点数比较特殊,必须使用浮点数宏进行比较

Fatal assertionNonfatal assertionVerifies
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

下面的宏,可以进行浮点数范围比较

Fatal assertionNonfatal assertionVerifies
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

测试代码

 运行结果

[ RUN      ] HelloGtest.case_EXPECT_FLOAT_EQ
[       OK ] HelloGtest.case_EXPECT_FLOAT_EQ (0 ms)
[ RUN      ] HelloGtest.case_EXPECT_NEAR
[       OK ] HelloGtest.case_EXPECT_NEAR (0 ms)
 命中[ 4 ]总行数[ 10 ]覆盖率[40.0%] 

EXPECT_DEATH
EXPECT_EXIT

Death Test终止测试

使用场景:检验程序的退出码,退出信号,退出时打印的字符串

终止测试的宏如下

Fatal assertionNonfatal assertionVerifies
ASSERT_DEATH(statement, matcher);EXPECT_DEATH(statement, matcher);statement crashes with the given error
ASSERT_DEATH_IF_SUPPORTED(statement, matcher);EXPECT_DEATH_IF_SUPPORTED(statement, matcher);if death tests are supported, verifies that statement crashes with the given error; otherwise verifies nothing
ASSERT_DEBUG_DEATH(statement, matcher);EXPECT_DEBUG_DEATH(statement, matcher);statement crashes with the given error in debug mode. When not in debug (i.e. NDEBUG is defined), this just executes statement
ASSERT_EXIT(statement, predicate, matcher);EXPECT_EXIT(statement, predicate, matcher);statement exits with the given error, and its exit code matches predicate

设置期待程序退出时的exit code

ExitedWithCode(exit_code)

设置程序退出时收到的Signal (只支持在Linux上使用)

KilledBySignal(signal_number)  // Not available on Windows.

字符串检查支持正则表达式

ExpressionMeaning
cmatches any literal character c
\\dmatches any decimal digit
\\Dmatches any character that's not a decimal digit
\\fmatches \f
\\nmatches \n
\\rmatches \r
\\smatches any ASCII whitespace, including \n
\\Smatches any character that's not a whitespace
\\tmatches \t
\\vmatches \v
\\wmatches any letter, _, or decimal digit
\\Wmatches any character that \\w doesn't match
\\cmatches any literal character c, which must be a punctuation
.matches any single character except \n
A?matches 0 or 1 occurrences of A
A*matches 0 or many occurrences of A
A+matches 1 or many occurrences of A
^matches the beginning of a string (not that of each line)
$matches the end of a string (not that of each line)
xymatches x followed by y

测试代码


运行结果

[ RUN      ] DeathTest.case_EXPECT_DEATH
[       OK ] DeathTest.case_EXPECT_DEATH (94 ms)
[ RUN      ] DeathTest.NormalExit
case_EXPECT_DEATH.c:21: Failure
Death test: GetValueFloat(3)
    Result: died but not with expected exit code:
            Exited with exit status 123
Actual msg:
[  DEATH   ] 
 命中[ 3 ]总行数[ 10 ]覆盖率[30.0%] 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值