Google Test
概述
测试用例工作空间独立,在 CaseA 中给成员变量赋值无法在 CaseB 中访问,使用TEST_F
尤其需要注意这个特点。
TEST
常用于测试互相独立的单个方法。TEST_F
常用于测试几个测试用例依赖同一份数据的情况。
1 |
|
1 |
|
断言
gtest 中,断言的宏可以理解为分为两类,一类是 ASSERT 系列,一类是 EXPECT 系列。
ASSERT_*
系列的断言,当检查点失败时,退出当前函数(注意:并非退出当前案例)。EXPECT_*
系列的断言,当检查点失败时,继续往下执行。
布尔值检查
Fatal assertion | Nonfatal assertion | Verifies |
---|---|---|
ASSERT_TRUE(condition); | EXPECT_TRUE(condition); | condition is true |
ASSERT_FALSE(condition); | EXPECT_FALSE(condition); | condition is false |
数值型数据检查
Fatal assertion | Nonfatal assertion | Verifies |
---|---|---|
ASSERT_EQ(expected, actual); | EXPECT_EQ(expected, actual); | expected == actual |
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 |
字符串检查
Fatal assertion | Nonfatal assertion | Verifies |
---|---|---|
ASSERT_STREQ(expected_str, actual_str); | EXPECT_STREQ(expected_str, actual_str); | the two C strings have the same content |
ASSERT_STRNE(str1, str2); | EXPECT_STRNE(str1, str2); | the two C strings have different content |
ASSERT_STRCASEEQ(expected_str, actual_str); | EXPECT_STRCASEEQ(expected_str, actual_str); | the two C strings have the same content, ignoring case |
ASSERT_STRCASENE(str1, str2); | EXPECT_STRCASENE(str1, str2); | the two C strings have different content, ignoring case |
Reference
Google Test
https://laplac2.github.io/tools/gtest/