gtest是Google的一套用于編寫C++測試的框架,可以運行在很多平臺上(包括Linux、Mac OS X、Windows、Cygwin等等)。基于xUnit架構。支持很多好用的特性,包括自動識別測試、豐富的斷言、斷言自定義、死亡測試、非終止的失敗、生成XML報告等等。
1.gtest優點
2.搭建測試框架
gtest下載地址:?GitHub - google/googletest: GoogleTest - Google Testing and Mocking Framework
下載方法是:git clone?GitHub - google/googletest: GoogleTest - Google Testing and Mocking Framework
安裝方法是:
$ cd googletest
注意:如果在make 過程中報錯,可在CMakeLists.txt 中增加如下行,再執行下面的命令: ?SET(CMAKE_CXX_FLAGS "-std=c++11")??
$ cmake .????
$ make
然后在lib目錄下會生成:libgmock.a libgmock_main.a libgtest.a libgtest_main.a
最后我們再sudo make install。
測試demo
目錄結構:
要測試的兩個函數如下:
simple1.cc
#include "sample1.h"// Returns n! (the factorial of n). For negative n, n! is defined to be 1.
int Factorial(int n) {int result = 1;for (int i = 1; i <= n; i++) {result *= i;}return result;
}// Returns true iff n is a prime number.
bool IsPrime(int n) {// Trivial case 1: small numbersif (n <= 1) return false;// Trivial case 2: even numbersif (n % 2 == 0) return n == 2;// Now, we have that n is odd and n >= 3.// Try to divide n by every odd number i, starting from 3for (int i = 3; ; i += 2) {// We only have to try i up to the square root of nif (i > n/i) break;// Now, we have i <= n/i < n.// If n is divisible by i, n is not prime.if (n % i == 0) return false;}// n has no integer factor in the range (1, n), and thus is prime.return true;
}
頭文件sample.h
#ifndef GTEST_SAMPLES_SAMPLE1_H_
#define GTEST_SAMPLES_SAMPLE1_H_// Returns n! (the factorial of n). For negative n, n! is defined to be 1.
int Factorial(int n);// Returns true iff n is a prime number.
bool IsPrime(int n);#endif
編寫測試類
1.包含頭文件gtest/gtest.h、limits.h(最大int和最小int)
2.格式
TEST(測試套名,測試用例名)
{//EXPECT_XX}
一個測試套下可以有多個測試用例
eg:
simple_unittest.cc
#include <limits.h>//包含最大最小數
#include "sample1.h"
#include "gtest/gtest.h"//gtest頭文件
namespace {TEST(FactorialTest, Negative) {// 測試名叫Negative,屬于FactorialTest測試套// 測試用例EXPECT_EQ(1, Factorial(-5));//斷言相等EXPECT_EQ(1, Factorial(-1));//前后順序不影響EXPECT_GT(Factorial(-10), 0);//斷言大于}TEST(FactorialTest, Zero) {EXPECT_EQ(1, Factorial(0));}TEST(FactorialTest, Positive) {EXPECT_EQ(1, Factorial(1));EXPECT_EQ(2, Factorial(2));EXPECT_EQ(6, Factorial(3));EXPECT_EQ(40320, Factorial(8));}// Tests IsPrime() 測試是否是素數TEST(IsPrimeTest, Negative) {//預測 true or falseEXPECT_FALSE(IsPrime(-1));EXPECT_FALSE(IsPrime(-2));EXPECT_FALSE(IsPrime(INT_MIN));}TEST(IsPrimeTest, Trivial) {EXPECT_FALSE(IsPrime(0));EXPECT_FALSE(IsPrime(1));EXPECT_TRUE(IsPrime(2));EXPECT_TRUE(IsPrime(3));}TEST(IsPrimeTest, Positive) {EXPECT_FALSE(IsPrime(4));EXPECT_TRUE(IsPrime(5));EXPECT_FALSE(IsPrime(6));EXPECT_TRUE(IsPrime(23));}
} // namespace
進行測試
不帶main
?實現測試的main函數,當然我們也可以不用寫main函數,那就需要連接gtest_main.a這個庫(編譯時添加-lgtest_main選項)。比如這樣子編譯:
g++ sample1.cc sample1_unittest.cc -lgtest -std=c++14?-lgtest_main -lpthread -o test1
執行結果:
帶main
固定寫法,將main補充在最后面
int main(int argc, char** argv)
{testing::InitGoogleTest(&argc,argv);return RUN_ALL_TESTS();
}
完整測試.cc:
#include <limits.h>//包含最大最小數
#include "sample1.h"
#include "gtest/gtest.h"//gtest頭文件
namespace {TEST(FactorialTest, Negative) {// 測試名叫Negative,屬于FactorialTest測試套// 測試用例EXPECT_EQ(1, Factorial(-5));//斷言相等EXPECT_EQ(1, Factorial(-1));//前后順序不影響EXPECT_GT(Factorial(-10), 0);//斷言大于}TEST(FactorialTest, Zero) {EXPECT_EQ(1, Factorial(0));}TEST(FactorialTest, Positive) {EXPECT_EQ(1, Factorial(1));EXPECT_EQ(2, Factorial(2));EXPECT_EQ(6, Factorial(3));EXPECT_EQ(40320, Factorial(8));}// Tests IsPrime() 測試是否是素數TEST(IsPrimeTest, Negative) {//預測 true or falseEXPECT_FALSE(IsPrime(-1));EXPECT_FALSE(IsPrime(-2));EXPECT_FALSE(IsPrime(INT_MIN));}TEST(IsPrimeTest, Trivial) {EXPECT_FALSE(IsPrime(0));EXPECT_FALSE(IsPrime(1));EXPECT_TRUE(IsPrime(2));EXPECT_TRUE(IsPrime(3));}TEST(IsPrimeTest, Positive) {EXPECT_FALSE(IsPrime(4));EXPECT_TRUE(IsPrime(5));EXPECT_FALSE(IsPrime(6));EXPECT_TRUE(IsPrime(23));}
} // namespaceint main(int argc, char** argv)
{testing::InitGoogleTest(&argc,argv);return RUN_ALL_TESTS();
}
編譯時去掉-lgtest_main選項: