目录
- 普通断言
- EXPECT_THAT
- 布尔条件断言
- EXPECT_TRUE
- EXPECT_FALSE
- 二进制断言
- EXPECT_EQ
- EXPECT_NE
- EXPECT_LT
- EXPECT_LE
- EXPECT_GT
- EXPECT_GE
- 字符串比较
- EXPECT_STREQ
- EXPECT_STRNE
- EXPECT_STRCASEEQ
- EXPECT_STRCASENE
- 浮点型比较
- EXPECT_FLOAT_EQ
- EXPECT_DOUBLE_EQ
- EXPECT_NEAR
- 显式成功与失败
- SUCCEED
- FAIL
- ADD_FAILURE
- ADD_FAILURE_AT
- 异常断言(未验证)
- EXPECT_THROW
- EXPECT_ANY_THROW
- EXPECT_NO_THROW
- 谓词断言
- EXPECT_PRED*
- EXPECT_PRED_FORMAT*
- 死亡断言
gtest中assert的头文件: gtest/gtest.h
EXPECT_和ASSERT_两种形式,EXPECT_产生非致命错误,ASSERT_产生致命错误并中断当前函数;所有断言宏支持使用重载<<的流输出
普通断言
EXPECT_THAT
todo
布尔条件断言
EXPECT_TRUE
EXPECT_TRUE(condition)
ASSERT_TRUE(condition)
验证condition是否为true,true为通过
EXPECT_FALSE
EXPECT_FALSE(condition)
ASSERT_FALSE(condition)
验证condition是否为false
二进制断言
用来比较两个值,参数中的两个值必须是可以比较的(支持比较运算符)
如果一个参数支持<<操作符,当比较失败时会被调用打印参数,否则gtest会用它认为最好的方式打印,具体参考https://google.github.io/googletest/advanced.html#teaching-googletest-how-to-print-your-values
Arguments are always evaluated exactly once, so it’s OK for the arguments to have side effects. However, the argument evaluation order is undefined and programs should not depend on any particular argument evaluation order.
支持宽字符(string)和窄字符(wstring)
浮点型数据不能比较
EXPECT_EQ
EXPECT_EQ(val1,val2)
ASSERT_EQ(val1,val2)
验证val1==val2,不相等则出错
EXPECT_NE
EXPECT_NE(val1,val2)
ASSERT_NE(val1,val2)
验证val1 != val2
如果是指针比较,对比的是内存中的地址值,而不是是否有相同的值。
use EXPECT_NE(ptr, nullptr) instead of EXPECT_NE(ptr, NULL)
EXPECT_LT
EXPECT_LT(val1,val2)
ASSERT_LT(val1,val2)
Verifies that val1<val2.
EXPECT_LE
EXPECT_LE(val1,val2)
ASSERT_LE(val1,val2)
Verifies that val1<=val2.
EXPECT_GT
EXPECT_GT(val1,val2)
ASSERT_GT(val1,val2)
Verifies that val1>val2.
EXPECT_GE
EXPECT_GE(val1,val2)
ASSERT_GE(val1,val2)
Verifies that val1>=val2.
字符串比较
这里的字符串比较用于两个C风格的字符串,如果对比两个string对象,使用 EXPECT_EQ or EXPECT_NE
o compare a C string with NULL, use EXPECT_EQ(c_string, nullptr) or EXPECT_NE(c_string, nullptr).
EXPECT_STREQ
EXPECT_STREQ(str1,str2)
ASSERT_STREQ(str1,str2)
Verifies that the two C strings str1 and str2 have the same contents.
EXPECT_STRNE
EXPECT_STRNE(str1,str2)
ASSERT_STRNE(str1,str2)
Verifies that the two C strings str1 and str2 have different contents.
EXPECT_STRCASEEQ
EXPECT_STRCASEEQ(str1,str2)
ASSERT_STRCASEEQ(str1,str2)
Verifies that the two C strings str1 and str2 have the same contents, ignoring case.
EXPECT_STRCASENE
EXPECT_STRCASENE(str1,str2)
ASSERT_STRCASENE(str1,str2)
Verifies that the two C strings str1 and str2 have different contents, ignoring case.
浮点型比较
EXPECT_FLOAT_EQ
EXPECT_FLOAT_EQ(val1,val2)
ASSERT_FLOAT_EQ(val1,val2)
Verifies that the two float values val1 and val2 are approximately equal, to within 4 ULPs from each other.
EXPECT_DOUBLE_EQ
EXPECT_DOUBLE_EQ(val1,val2)
ASSERT_DOUBLE_EQ(val1,val2)
Verifies that the two double values val1 and val2 are approximately equal, to within 4 ULPs from each other.
EXPECT_NEAR
EXPECT_NEAR(val1,val2,abs_error)
ASSERT_NEAR(val1,val2,abs_error)
Verifies that the difference between val1 and val2 does not exceed the absolute error bound abs_error.
显式成功与失败
这种是直接生成成功和失败,代替了表达式或者值。常用在控制流,而不是需要判断boolean的表达式来决定test的成功和失败,举例如下
switch(expression) {
case 1:
....
case 2:
....
default:
FATAL() << "We shouldn't get here.";
SUCCEED
SUCCEED()生成一个success,不会有用户可见的输出
FAIL
FAIL()生成一个fatal failure,从当前函数退出,只能用在返回值为void的函数中
ADD_FAILURE
ADD_FAILURE()生成一个nonfatal failure,它会允许当前函数继续执行
ADD_FAILURE_AT
ADD_FAILURE_AT(file_path,line_number) 在一个文件的指定行生成一条nonfatal failure
void test_func1() {
SUCCEED() << "------------gtest SUCCEED------------";
ADD_FAILURE() << "----------gtest ADD_FAILURE--------------";
ADD_FAILURE_AT("/home//iLearning/code/gtest/gtest_project/src/doc.txt", 2) << "--------gtest ADD_FAILURE_AT---------";
}
输出如下:
结果证明,SUCCEED()没有输出任何信息,ADD_FAILURE()和ADD_FAILURE_AT(file_path,line_number) 都有failure的输出,但ADD_FAILURE_AT并没有在一个文件的指定行生成一条nonfatal failure(不管这个文件在测试程序运行前是否存在)可能是理解错了?
异常断言(未验证)
EXPECT_THROW
EXPECT_THROW(statement,exception_type)
ASSERT_THROW(statement,exception_type)
验证statement会抛出类型为exception_type的异常
EXPECT_ANY_THROW
EXPECT_ANY_THROW(statement)
ASSERT_ANY_THROW(statement)
验证statement会抛出任何类型的异常
EXPECT_NO_THROW
EXPECT_NO_THROW(statement)
ASSERT_NO_THROW(statement)
验证statement不会抛出异常
谓词断言
EXPECT_PRED*
EXPECT_PRED1(pred,val1)
EXPECT_PRED2(pred,val1,val2)
EXPECT_PRED3(pred,val1,val2,val3)
EXPECT_PRED4(pred,val1,val2,val3,val4)
EXPECT_PRED5(pred,val1,val2,val3,val4,val5)
ASSERT_PRED1(pred,val1)
ASSERT_PRED2(pred,val1,val2)
ASSERT_PRED3(pred,val1,val2,val3)
ASSERT_PRED4(pred,val1,val2,val3,val4)
ASSERT_PRED5(pred,val1,val2,val3,val4,val5)
将给定的参数传给谓词pred,如果pred返回true则断言成功,否则失败,如果失败了,它会打印每个参数值。pred是一个函数或者函数对象,可以接受在断言宏中给定的其他参数。
bool MutuallyPrime(int m, int n) {
if (m > n) {
return true;
}
return false;
}
void test_pred_func1() {
int a = 3;
int b = 4;
int c = 10;
EXPECT_PRED2(MutuallyPrime, a, b);
EXPECT_PRED2(MutuallyPrime, c, a);
}
输出如下,断言失败会有打印,成功没有任何输出
注意:如果谓词是重载函数或者模板函数,断言宏可能无法识别,需要显式指定函数参数类型
重载函数:
IsPositive重载,int和double两个参数,需要如下写法
EXPECT_PRED1(static_cast<bool (*)(int)>(IsPositive), 5);
EXPECT_PRED1(static_cast<bool (*)(double)>(IsPositive), 3.14);
模板函数:
template <typename T>
bool IsNegative(T x) {
return x < 0;
}
// 需要这样写
ASSERT_PRED2((MyPredicate<int, int>), 5, 0);
EXPECT_PRED_FORMAT*
todo
死亡断言
todo