1. 安装gtest
采用源码安装的方式,需确保cmake
已经安装。
git clone https://github.com/google/googletest
cd googletest
mkdir build
cd build
cmake ..
make
sudo make install
2. 测试安装成功
新建gtest.c文件,输入如下测试代码:
#include<gtest/gtest.h>
int add(int a,int b){
return a+b;
}
TEST(testCase,test0){
EXPECT_EQ(add(2,3),5);
}
int main(int argc,char **argv){
testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}
编译可执行文件
g++ gtest.c -lgtest -lpthread -v -o gtest
运行
./gtest
运行结果
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from testCase
[ RUN ] testCase.test0
[ OK ] testCase.test0 (0 ms)
[----------] 1 test from testCase (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.
以上运行结果代表gtest安装成功