GTEST_ASSERT_EQ
时间: 2023-07-14 21:01:51 浏览: 70
GTEST_ASSERT_EQ是Google Test框架中的一个断言宏。它用于在测试中进行断言比较。它的语法如下:
```
GTEST_ASSERT_EQ(expected, actual);
```
其中,expected表示预期值,actual表示实际值。如果两者相等,则断言通过;如果不相等,则断言失败,并输出详细的错误信息。
这个宏用于在测试中进行相等比较,如果相等则测试通过,否则测试失败。在Google Test中还有其他类型的断言宏,如GTEST_ASSERT_NE(不等比较)、GTEST_ASSERT_LT(小于比较)、GTEST_ASSERT_LE(小于等于比较)、GTEST_ASSERT_GT(大于比较)、GTEST_ASSERT_GE(大于等于比较)等。这些断言宏可以帮助我们编写更加健壮的测试用例。
相关问题
gtest结构体断言
gtest中的结构体断言主要有两种形式:ASSERT_EQ和EXPECT_EQ。其中,ASSERT_EQ在断言失败时会直接退出当前函数,而EXPECT_EQ则会继续执行。这两种形式都可以用于比较两个结构体是否相等。例如:
```
struct Person {
std::string name;
int age;
};
TEST(StructTest, Demo) {
Person p1 = {"Tom", 20};
Person p2 = {"Tom", 20};
ASSERT_EQ(p1, p2);
}
```
在上面的例子中,ASSERT_EQ会比较p1和p2是否相等,如果不相等则会直接退出当前函数。如果使用EXPECT_EQ,则会继续执行后面的代码。
GTest常用api介绍
Sure! GTest (Google Test) is a popular C++ testing framework developed by Google. Some commonly used APIs in GTest are:
- TEST(): This macro is used to define a new test case.
- ASSERT_EQ(), EXPECT_EQ(): These macros are used to compare two values and fail the test if they are not equal. ASSERT_EQ() aborts the current test function immediately, while EXPECT_EQ() continues the test function.
- ASSERT_TRUE(), EXPECT_TRUE(): These macros check if an expression is true and fail if it is false.
- ASSERT_FALSE(), EXPECT_FALSE(): These macros check if an expression is false and fail if it is true.
- SetUp() and TearDown(): These functions are called before and after each test case to set up and tear down testing contexts.
- SCOPED_TRACE(): This macro prints out additional information when an assertion fails to help diagnose the issue.
Hope this helps!