1. 安装
##### Ubuntu:
1 apt-get install libgtest-dev # 执行后源码下载到/usr/src/gtest目录下 2 cd /usr/src/gtest 3 mkdir build && cd build && cmake .. && make 4 cp libgtest*.a /usr/local/lib # 开发环境下还需要拷贝头文件到/usr/local/include
2. 用例
1 // demo.cpp g++ demo.cpp -lgtest -lpthread 2 #include <gtest/gtest.h> 3 4 // 最小公倍数 5 int LCM(int m, int n) 6 { 7 int max = 0, min = 0, temp = 0; 8 9 m > n ? (max = m, min = n):(max = n, min = m); 10 11 while(min != 0) { 12 temp = max%min; 13 max = min; 14 min = temp; 15 } 16 17 return m*n/max; 18 } 19 20 TEST(LCMTest, LCMTestInput) 21 { 22 EXPECT_EQ(20, LCM(4, 10)); 23 EXPECT_EQ(90, LCM(30, 18)); 24 } 25 26 int main(int argc, char *argv[]) 27 { 28 testing::InitGoogleTest(&argc, argv); 29 return RUN_ALL_TESTS(); 30 }
3. 断言说明