如果你還沒有搭建gtest框架,可以參考我之前的博客:http://www.cnblogs.com/jycboy/p/6001153.html。。
你把github上的項目導來之後,github地址:https://github.com/google/googletest,在目錄:..(你的目錄)\googletest-master\googletest\samples是你的samples文件夾。
在VS中創建項目:GtestSamples
把對應的代碼加入到這裡邊:sample1.h、sample1.cc、sample1_unittest.cc.
在sample1.cc中是你要測試的函數:
// 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 numbers if (n <= 1) return false; // Trivial case 2: even numbers if (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 3 for (int i = 3; ; i += 2) { // We only have to try i up to the squre root of n if (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; }
在sample1_unittest.cc中,是你編寫的測試:
有兩個測試用例:Factorial、IsPrime;每個測試用例對應三個test。
extern int Factorial(int n); extern bool IsPrime(int n); // Tests Factorial(). // Tests factorial of negative numbers. TEST(FactorialTest, Negative) { // This test is named "Negative", and belongs to the "FactorialTest" // test case. EXPECT_EQ(1, Factorial(-5)); EXPECT_EQ(2, Factorial(-1)); //如果是ASSERT_EQ,後邊的EXPECT_GT將不在執行;如果是EXPECT_EQ,後邊的測試EXPECT_GT繼續執行 EXPECT_GT(Factorial(-10), 0); ASSERT_EQ(3, Factorial(-1)); // <TechnicalDetails> // // EXPECT_EQ(expected, actual) is the same as // // EXPECT_TRUE((expected) == (actual)) // // except that it will print both the expected value and the actual // value when the assertion fails. This is very helpful for // debugging. Therefore in this case EXPECT_EQ is preferred. // // On the other hand, EXPECT_TRUE accepts any Boolean expression, // and is thus more general. // // </TechnicalDetails> } // Tests factorial of 0. TEST(FactorialTest, Zero) { EXPECT_EQ(1, Factorial(0)); } // Tests factorial of positive numbers. 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() // Tests negative input. TEST(IsPrimeTest, Negative) { // This test belongs to the IsPrimeTest test case. EXPECT_FALSE(IsPrime(-1)); EXPECT_FALSE(IsPrime(-2)); EXPECT_FALSE(IsPrime(INT_MIN)); } // Tests some trivial cases. TEST(IsPrimeTest, Trivial) { EXPECT_FALSE(IsPrime(0)); EXPECT_FALSE(IsPrime(1)); EXPECT_TRUE(IsPrime(2)); EXPECT_TRUE(IsPrime(3)); } // Tests positive input. TEST(IsPrimeTest, Positive) { EXPECT_FALSE(IsPrime(4)); EXPECT_TRUE(IsPrime(5)); EXPECT_FALSE(IsPrime(6)); EXPECT_TRUE(IsPrime(23)); } // Step 3. Call RUN_ALL_TESTS() in main(). // // We do this by linking in src/gtest_main.cc file, which consists of // a main() function which calls RUN_ALL_TESTS() for us. // // This runs all the tests you've defined, prints the result, and // returns 0 if successful, or 1 otherwise. // // Did you notice that we didn't register the tests? The // RUN_ALL_TESTS() macro magically knows about all the tests we // defined. Isn't this convenient?
Step 1.包括必要的頭文件,以便聲明測試邏輯需要的東西。不要忘了 gtest.h
#include <limits.h> #include "sample1.h" #include <gtest/gtest.h>
Step 2. 使用TEST宏來定義測試。
TEST有兩個參數:測試用例名稱和測試名稱。
使用宏後,應該在一對大括號之間定義測試邏輯。 您可以使用一堆宏來指示測試的成功或失敗。 EXPECT_TRUE和EXPECT_EQ是此類宏的示例。 有關完整列表,請參閱gtest.h。
技術細節:
在Google Test中,測試分為多個測試用例。你應該將邏輯相關的測試放入同一個測試用例。
測試用例名和測試名都應該是有效的C ++標識符。 並且你不應該在名稱中使用下劃線(_)。
Google測試保證您定義的每個測試只運行一次,但不能保證測試執行的順序。 因此,您應該以這樣一種方式編寫測試,使得它們的結果不依賴於它們的順序。
TEST(FactorialTest, Negative) { // This test is named "Negative", and belongs to the "FactorialTest" // test case. EXPECT_EQ(1, Factorial(-5)); EXPECT_EQ(2, Factorial(-1)); //如果是ASSERT_EQ,後邊的EXPECT_GT將不在執行;如果是EXPECT_EQ,後邊的測試EXPECT_GT繼續執行 EXPECT_GT(Factorial(-10), 0); EXPECT_TRUE(2 == Factorial(-7));//這個錯誤失敗信息打印的是true或false,不會打印值 ASSERT_EQ(3, Factorial(-1)); }
技術細節:
上面的EXPECT_EQ(1, Factorial(-1))和EXPECT_TRUE(1==Factorial(-1))的作用是一樣的,但是當失敗時,EXPECT_EQ會打印期望值和實際值,而EXPECT_TRUE是會打印true、false。所以優先選用EXPECT_EQ。
Step 3. Call RUN_ALL_TESTS() in main().
int main(int argc, char **argv) { //printf("Running main() from gtest_main.cc\n"); testing::InitGoogleTest(&argc,argv); return RUN_ALL_TESTS(); }
**RUN_ALL_TESTS() 會自動調用所有的測試。
之前的兩篇博客:
之後會陸續更新,一直到GMock,但願我不會太懶,。。。。