一.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
此時可看到上述單元測試的執行結果:
說明兩個測試案例均成功執行,具體檢測語法可參見相關教程。