一.Ubuntu中安装Gtest
依次使用以下指令即可安装gtest:
$ git clone https://github.com/google/googletest.git $ cd googletest $ mkdir build $ cd build $ cmake .. $ make $ sudo make install
以下验证gtest是否能够使用:
1,在某个文件夹中,新建test.cpp文件,输入以下代码:
#include <gtest/gtest.h>
int Foo(int a,int b)
{
if(0 == a||0 == b)
throw "don't do that";
int c = a%b;
if (0 == c)
{
return b;
}
return Foo(b,c);
}
TEST(FooTest,HandleNoneZeroInput)
{
EXPECT_EQ(2,Foo(4,10));
EXPECT_EQ(6,Foo(30,18));
}
int main(int argc,char*argv[])
{
testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}
2,在命令终端编译构建该文件:
$ g++ -std=c++11 test.cpp -lgtest -lpthread
$ ./a.out
此时可看到上述单元测试的执行结果:

说明两个测试案例均成功执行,具体检测语法可参见相关教程。
