外部依赖项:
liggtest.a libgtest_main.a 相关头文件
Main函数写法参考:
int main(int argc,char* argv[])
{
//testing::GTEST_FLAG(output) = "xml:"; //生成xml结果文件
testing::InitGoogleTest(&argc,argv); //初始化
if( RUN_ALL_TESTS()) //跑单元测试
return 0;
}
1.断言的使用
1)ASSERT_* 断言
ASSERT_*系列断言,当检查点失败时,退出当前函数
例如:有如下两个测试用例:
TEST(unit,assert_t_1)
{
ASSERT_EQ(4,Add(1,2));
ASSERT_EQ(3,Add(1,2));
}
TEST(unit,assert_t_2)
{
ASSERT_EQ(4,Add(1,3));
}
输出结果:
main startinit[==========] Running 2 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 2 tests from unit
[ RUN ] unit.assert_t_1
unittest.cpp:5: Failure
Expected equality of these values:
4
Add(1,2)
Which is: 3
[ FAILED ] unit.assert_t_1 (0 ms)
[ RUN ] unit.assert_t_2
[ OK ] unit.assert_t_2 (0 ms)
[----------] 2 tests from unit (0 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.
[ FAILED ] 1 test, listed below:
[ FAILED ] unit.assert_t_1
分析:
上面实际上只跑了两个用例,当 assert_t_1 测试第一个测试点测试失败时,整个函数都退出,转向执行第二个函数。
2)EXPECT_* 断言
EXPECT_* 断言当检查点失败时,会继续往下执行
例如:如下俩个测试用例:
TEST(unitqxcept,except_t_1)
{
std::cout<<"first point\n";
EXPECT_EQ(4,Add(1,2));
st