1.gtest TEST宏
TEST(test_case_name, test_name)
TEST_F(test_fixture,test_name) //多个测试场景需要相同数据配置的情况,用TEST_F。TEST_F test fixture,测试夹具,测试套,承担了一个注册的功能。
2.gtest断言
gtest断言有两种,一类是ASSERT宏,另一类就是EXPECT宏了。
a ASSERT,断言失败会退出,不继续执行
b EXPECT,断言失败会继续执行
如果你对自动输出的错误信息不满意的话,也是可以通过operator<<能够在失败的时候打印日志,将一些自定义的信息输出。
2.1 布尔断言
ASSERT | EXCEPT | 备注 |
---|---|---|
ASSERT_TRUE(condition) | EXCEPT_TRUE(condition) | condition为true时正常 |
ASSERT_FALSE(condition) | EXCEPT_FALSE(condition) | condition为false时正常 |
2.2 数值
ASSERT | EXCEPT | 备注 |
---|---|---|
ASSERT_EQ(expected,actual) | EXCEPT_EQ(expected,actual) | expected=actual |
ASSERT_NE(expected,actual) | EXCEPT_NE(expected,actual) | expected!=actual |
ASSERT_LT(expected,actual) | EXCEPT_LT(expected,actual) | expected<actual |
ASSERT_LE(expected,actual) | EXCEPT_LE(expected,actual) | expected<=actual |
ASSERT_GT(expected,actual) | EXCEPT_GT(expected,actual) | expected>actual |
ASSERT_GE(expected,actual) | EXCEPT_GE(expected,actual) | expected>=actual |
2.3字符串
ASSERT | EXCEPT | 备注 |
---|---|---|
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) | 两个字符串有不同的内容,忽略大小写 |
2.4异常检查
ASSERT | EXCEPT | 备注 |
---|---|---|
ASSERT_THROW(statement, exception_type) | EXPECT_THROW(statement, exception_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 |
2.5浮点数
ASSERT | EXCEPT | 备注 |
---|---|---|
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 |
2.6相近的两个数
ASSERT | EXCEPT | 备注 |
---|---|---|
ASSERT_NEAR(val1, val2, abs_error) | EXPECT_NEAR(val1, val2, abs_error) | the difference between val1 and val2 doesn’t exceed the given absolute error |
3 gtest事件机制
3.1TestSuite
我们需要写一个类,继承testing::Test,然后实现两个静态方法
- SetUpTestCase() 方法在第一个TestCase之前执行
- TearDownTestCase() 方法在最后一个TestCase之后执行
在编写测试案例时,我们需要使用TEST_F这个宏,第一个参数必须是我们上面类的名字,代表一个TestSuite。
3.2TestCase
TestCase事件是挂在每个案例执行前后的,实现方式和上面的几乎一样,不过需要实现的是SetUp方法和TearDown方法:
- SetUp()方法在每个TestCase之前执行
- TearDown()方法在每个TestCase之后执行
3.3 全局事件
要实现全局事件,必须写一个类,继承testing::Environment类,实现里面的SetUp和TearDown方法。
-
SetUp()方法在所有案例执行前执行
-
TearDown()方法在所有案例执行后执行
还需要告诉gtest添加这个全局事件,我们需要在main函数中通过testing::AddGlobalTestEnvironment方法将事件挂进来,也就是说,我们可以写很多个这样的类,然后将他们的事件都挂上去。
#include <gtest/gtest.h>
#include <string>
class Student{
public:
bool init(){return true;}
bool uninit(){return false;}
int add(){return 0;}
std::string get(){return "";}
};
class Test1:public ::testing::Test
{
public:
void SetUp() override {
}
void TearDown override {}
public:
Student m_student;
};
TEST_F(Test1,Case1_bool)
{
ASSERT_TRUE(m_student.init());
EXCEPT_TRUE(m_student.init());
ASSERT_FALSE(m_student.uninit());
EXCEPT_FALSE(m_student.uninit());
}
TEST_F(Test1,Case2_int)
{
ASSERT_EQ(0,m_student.add());
EXCEPT_EQ(0,m_student.add());
}
TEST_F(Test1,Case3_string)
{
ASSERT_STREQ("",m_student.get());
EXCEPT_STREQ("",m_student.get());
}
4编译运行
cmake … -DBUILD_TEST=ON
make
make test
参考如下
https://www.cnblogs.com/helloworldcode/p/9606838.html