gtest学习之断言

gtest中,断言的宏可以理解分为两类:一类是ASSERT系列,另一类是EXPECT系列。

ASSERT系列的断言----当检查点失败时,退出当前函数;

EXPECT系列的断言----当检查点失败时,继续往下执行。

样例:

//int型比较,预期值:3,实际值:Add(1, 2)
EXPECT_EQ(3, Add(1, 2)
如果Add(1, 2)的结果为5时,会在结果中输出:
g:\myproject\c++\gtestdemo\gtestdemo\gtestdemo.cpp(16): error: Value of: Add(1, 2)
Actual: 4
Expected:3


布尔值检查的宏:
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(expectedactual)
EXPECT_EQ(expectedactual)
expected == actual
ASSERT_NE(val1val2)
EXPECT_NE(val1val2)
val1 != val2
ASSERT_LT(val1val2)
EXPECT_LT(val1val2)
val1 < val2
ASSERT_LE(val1val2)
EXPECT_LE(val1val2)
val1 <= val2
ASSERT_GT(val1val2)
EXPECT_GT(val1val2)
val1 > val2
ASSERT_GE(val1val2)
EXPECT_GE(val1val2)
val1 >= val2

字符串检查:

Fatal assertion
Nonfatal assertion
Verifies
ASSERT_STREQ(expected_stractual_str)
EXPECT_STREQ(expected_stractual_str)
the two C strings have the same content
ASSERT_STRNE(str1str2)
EXPECT_STRNE(str1str2)
the two C strings have different content
ASSERT_STRCASEEQ(expected_stractual_str)
EXPECT_STRCASEEQ(expected_stractual_str)
the two C strings have the same content, ignoring case
ASSERT_STRCASENE(str1str2)
EXPECT_STRCASENE(str1str2)
the two C strings have different content, ignoring case

显示返回成功或失败

Fatal assertion
Nonfatal assertion
FAIL()
ADD_FAILURE()

异常检查:

Fatal assertion Nonfatal assertion Verifies
ASSERT_THROW(statementexception_type) EXPECT_THROW(statementexception_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

检查浮点数:

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


Windows HRESULT assertions

Fatal assertion Nonfatal assertion Verifies
ASSERT_HRESULT_SUCCEEDED(expression) EXPECT_HRESULT_SUCCEEDED(expression) expression is a success HRESULT
ASSERT_HRESULT_FAILED(expression) EXPECT_HRESULT_FAILED(expression) expression is a failure HRESULT




你可能感兴趣的:(C++单元测试框架gtest)