gtest assertion

1 Introduction

  gtest, the assert macro can be understood as divided into two categories, one is ASSERT series, one is EXPECT series.
Difference:
ASSERT_ * series of assertions, when the checkpoint failed to exit the current function (note: not exit the current case)
EXPECT_ * series of assertions, when the checkpoint fails, continue down the implementation. Outputs expected and actual values in the result, can be exported in an XML file

2, custom information output

  Automatic output of error messages are not satisfied, you can also customize some of the information output by the << operator. Without the use of << operator to customize the output, the current view in the cycle than the expected value cycle, such as:
  EXPECT_EQ (X [I], Y [I]);
result:
Actual:. 4
the Expected: X [I]
Which is: 3

<< operator using custom outputs such as:
EXPECT_EQ (X [I], Y [I]) << "Vectors X and Y Differ AT index" << I;
result: Actual:. 4
the Expected: X [I]
IS Which:. 3
Vectors X and Y index Differ AT 2

3, Boolean

  ASSERT_TRUE (condition); EXPECT_TRUE (condition ); the condition is true
  ASSERT_FALSE (condition); EXPECT_FALSE (condition ); the condition is false

4, numerical data checking

  ASSERT_EQ(expected, actual); EXPECT_EQ(expected, actual); 判断==
  ASSERT_NE(val1, val2); EXPECT_NE(val1, val2); 判断 !=
  ASSERT_LT(val1, val2); EXPECT_LT(val1, val2); 判断 <
  ASSERT_LE(val1, val2); EXPECT_LE(val1, val2); 判断 <=
  ASSERT_GT(val1, val2); EXPECT_GT(val1, val2); 判断 >
  ASSERT_GE(val1, val2); EXPECT_GE(val1, val2); 判断 >=

5, the string is checked

  ASSERT_STREQ (expected_str, actual_str); EXPECT_STREQ (expected_str, actual_str); the contents of the same string
  ASSERT_STRNE (str1, str2); EXPECT_STRNE (str1, str2); different content strings
  ASSERT_STRCASEEQ (expected_str, actual_str); EXPECT_STRCASEEQ (expected_str, actual_str ); the contents of the same string, ignoring case
  ASSERT_STRCASENE (str1, str2); EXPECT_STRCASENE (str1, str2); different contents of the string, ignoring case

  * STREQ * and * STRNE * Supports char * and wchar_t * type, * STRCASEEQ * and * STRCASENE * but only receives char * .

6, the display returns success or failure

  Direct return success: SUCCEED ();
  returns failure:
    FAIL (); // do not execute down the case
    ADD_FAILURE (); // continue down

7, abnormal inspection

  ASSERT_THROW (statement, exception_type); EXPECT_THROW (statement, exception_type); statement given type of exception initiator

  ASSERT_ANY_THROW (statement); EXPECT_ANY_THROW (statement ); statements raise any type of exception
  ASSERT_NO_THROW (statement); EXPECT_NO_THROW (statement ); statement does not throw any exception

8, Predicate Assertions assert predicate

  When using EXPECT_TRUE or ASSERT_TRUE, sometimes you want to be able to output more detailed information, such as when checking the return value of TRUE or FALSE a function, what hope can output parameter is passed, in order to better track after the failure.

  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
  .... .. ...

只提供<=5个参数的,如果需要测试更多的参数,直接告知。

8.1 自定义的输出格式

  ASSERT_PRED_FORMAT1(pred_format1, val1);` EXPECT_PRED_FORMAT1(pred_format1, val1); pred_format1(val1) 成功
  ASSERT_PRED_FORMAT2(pred_format2, val1, val2); EXPECT_PRED_FORMAT2(pred_format2, val1, val2); pred_format2(val1, val2) 成功

8.2 用法

1)定义1个信息
  testing::Message msg;
2)使用<<操作符定义信息
  msg << m_expr << " 和 " << n_expr << " 的最大公约数应该是:" << Foo(m, n) << " 而不是:" << k_expr;
3)返回 testing::AssertionFailure(msg);
4)在TEST宏中使用
  EXPECT_PRED_FORMAT3(AssertFoo, 3, 6, 2);

总的例子:
testing::AssertionResult AssertFoo(const char* m_expr, const char* n_expr, const char* k_expr, int m, int n, int k) {
if (Foo(m, n) == k)
return testing::AssertionSuccess();
testing::Message msg;
msg << m_expr << " 和 " << n_expr << " 的最大公约数应该是:" << Foo(m, n) << " 而不是:" << k_expr;
return testing::AssertionFailure(msg);
}

TEST(AssertFooTest, HandleFail)
{
EXPECT_PRED_FORMAT3(AssertFoo, 3, 6, 2);
}

9、浮点型检查

  ASSERT_FLOAT_EQ(expected, actual); EXPECT_FLOAT_EQ(expected, actual); 两个浮点值几乎相等
  ASSERT_DOUBLE_EQ(expected, actual); EXPECT_DOUBLE_EQ(expected, actual); 两个双精度值几乎相等

对相近的两个数的比较:
  ASSERT_NEAR(val1, val2, abs_error); EXPECT_NEAR(val1, val2, abs_error); val1和val2之间的差异不超过给定的绝对误差

还可使用:
  EXPECT_PRED_FORMAT2(testing::FloatLE, val1, val2);
  EXPECT_PRED_FORMAT2(testing::DoubleLE, val1, val2);

10、Windows HRESULT断言

  ASSERT_HRESULT_SUCCEEDED(expression); EXPECT_HRESULT_SUCCEEDED(expression); 表达式返回的是成功的HRESULT
  ASSERT_HRESULT_FAILED(expression); EXPECT_HRESULT_FAILED(expression); 表达式返回的是失败的HRESULT

11、类型检查

template <typename T> class FooType {
public:
void Bar() { testing::StaticAssertTypeEq<int, T>(); }
};

TEST(TypeAssertionTest, Demo)
{
FooType<bool> fooType;
fooType.Bar();
}

Guess you like

Origin www.cnblogs.com/Sheenagh/p/12215179.html