GoogleTest(断言)-2

googletest中,断言的宏可以理解为分为两类,一类是ASSERT系列,一类是EXPECT系列。一个直观的解释就是:

  1. ASSERT_* 系列的断言,当检查点失败时,退出当前函数(注意:并非退出当前案例)。
  2. EXPECT_* 系列的断言,当检查点失败时,继续往下执行。

基本断言

这些断言执行基本的 true/false 条件测试。

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

比较两个值的断言

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

值参数必须是断言的比较操作符可比较的,否则将报出编译错误。

例:

// int型比较,预期值:4,实际值:Add(1, 2)  
EXPECT_EQ(4, Add(1, 2))  

结果:

g:\myproject\c++\gtestdemo\gtestdemo\gtestdemo.cpp(16): error: Value of: Add(1, 2)  
  Actual: 3  
Expected:4  

如果你对自动输出的出错信息不满意的话,你还可以通过操作符<<将一些自定义的信息输出,通常,这对于调试或是对一些检查点的补充说明来说。
例:

for (int i = 0; i < x.size(); ++i)  
{  
    EXPECT_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i;  
}  

结果:

g:\myproject\c++\gtestdemo\gtestdemo\gtestdemo.cpp(25): error: Value of: y[i]  
  Actual: 4  
Expected: x[i]  
Which is: 3  
Vectors x and y differ at index 2   

布尔值

Fatal assertion Nonfatal assertion Verifies
ASSERT_TRUE(condition) EXPECT_TRUE(condition) condition 是 true
ASSERT_FALSE(condition) EXPECT_FALSE(condition) condition 是 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) 这两个C字符串具有相同的内容
ASSERT_STRNE(str1, str2) EXPECT_STRNE(str1, str2) 这两个C字符串具有不同的内容
ASSERT_STRCASEEQ(expected_str, actual_str) EXPECT_STRCASEEQ(expected_str, actual_str) 这两个C字符串的内容相同,忽略大小写
ASSERT_STRCASENE(str1, str2) EXPECT_STRCASENE(str1, str2) 这两个C字符串的内容不同,忽略大小写

异常检查

Fatal assertion Nonfatal assertion Verifies
ASSERT_THROW(statement, exception_type) EXPECT_THROW(statement, exception_type) 语句引发给定类型的异常
ASSERT_ANY_THROW(statement) EXPECT_ANY_THROW(statement) statement 语句引发任何类型的异常。
ASSERT_NO_THROW(statement) EXPECT_NO_THROW(statement) 语句不会引发任何异常

Predicate Assertions(谓词断言)

在使用EXPECT_TRUE或ASSERT_TRUE时,有时希望能够输出更加详细的信息,比如检查一个函数的返回值TRUE还是FALSE时,希望能够输出传入的参数是什么,以便失败后好跟踪。因此提供了如下的断言:

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

例:

bool MutuallyPrime(int m, int n)  
{  
    return Foo(m , n) > 1;  
}  

TEST(PredicateAssertionTest, Demo)  
{  
    int m = 5, n = 6;  
    EXPECT_PRED2(MutuallyPrime, m, n);  
}    

显示:

error: MutuallyPrime(m, n) evaluates to false, where  
m evaluates to 5  
n evaluates to 6  

浮点型

Fatal assertion Nonfatal assertion Verifies
ASSERT_FLOAT_EQ(expected, actual) EXPECT_FLOAT_EQ(expected, actual) 这两个float几乎相等
ASSERT_DOUBLE_EQ(expected, actual) EXPECT_DOUBLE_EQ(expected, actual) 这两个double几乎相等

对相近的两个数比较

Fatal assertion Nonfatal assertion Verifies
ASSERT_NEAR(val1, val2, abs_error) EXPECT_NEAR(val1, val2, abs_error) Val1和val2之间的差额不超过给定的绝对误差

文章参考:https://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html

你可能感兴趣的:(GoogleTest(断言)-2)