gtest 代碼目錄結構
說明:以 gtest-1.7.0 爲例
cmake, codegear, make, msvc, xcode
構建測試項目的構建文件,如 make 就是 Makefile.
To write a test program using Google Test, you need to compile Google Test into a library and link your test with it. We provide build files for some popular build systems: msvc/ for Visual Studio, xcode/ for Mac Xcode, make/ for GNU make, codegear/ for
Borland C++ Builder, and the autotools script (deprecated) and CMakeLists.txt for CMake (recommended) in the Google Test root directory.
include, src
分別爲 gtest 框架實現代碼的頭文件和 C++ 文件。
fused-src
將上面 include 和 src 中多個 .h, .cc 文件合併成一個 .h, .cc 之後的代碼。
Google Test's implementation consists of ~30 files (excluding its own tests). Sometimes you may want them to be packaged up in two files (a .h and a .cc) instead, such that you can easily copy them to a new machine and start hacking there.
samples
gtest 提供的使用例子。
https://code.google.com/p/googletest/wiki/Samples
TEST: sample1, sample2,sample4. 多個用例之間無共享代碼。
TEST_F: sample3, sample5. 多個用例之間共享代碼,需要繼承 testing::Test.
test
gtest 的測試代碼。(用 gtest 測試 gtest)
gtest 的原理及如何使用
gtest 是一個框架(其實就是庫),一切測試都是通過“宏”來完成的,例如 TEST(), TEST_F(), ASSERT_TRUE() 等,基本邏輯是:
gtest 定義測試用的宏,測試代碼使用宏調用被測試的函數,下面是一個完整的測試項目的組成部分:
//////////////////////////////////////////////////////////
// Section 1: gtest
#define TEST(...) ...
//////////////////////////////////////////////////////////
// Section 2: tested function
function()
{
...
}
//////////////////////////////////////////////////////////
// Section 3: test case(using "macro" in gtest and
// testing data to test function)
TEST(function)
{
...
}
//////////////////////////////////////////////////////////
// Section 4: main
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
以 samples 中的 sample1 來說:
sample1.h, sample.cc: section 2
sample1_unittest.cc: section 3
fused-src/gtest/gtest_main.cc: section 4(這部分代碼也可以自己寫)
編譯鏈接流程
因此構建一個測試項目時需要編譯:
1. samples/sample.cc
2. samples/sample1_unittest.cc
3. fused-src/gtest/gtest-all.cc: 實現宏的代碼(庫)
4. fused-src/gtest/gtest_main.cc: 主函數
注意:實際上 sample 使用的是 include 和 src 中的代碼,這裏爲了方便說明用 fused-src 中的代碼代替
編譯完之後鏈接至 main() 即可生成可執行文件。
make/Makefile 演示了這一過程:
g++ -isystem ../include -g -Wall -Wextra -pthread -c ../samples/sample1.cc
g++ -isystem ../include -g -Wall -Wextra -pthread -c ../samples/sample1_unittest.cc
g++ -isystem ../include -I.. -g -Wall -Wextra -pthread -c ../src/gtest-all.cc
g++ -isystem ../include -I.. -g -Wall -Wextra -pthread -c ../src/gtest_main.cc
ar rv gtest_main.a gtest-all.o gtest_main.o
g++ -isystem ../include -g -Wall -Wextra -pthread -lpthread sample1.o sample1_unittest.o gtest_main.a -o sample1_unittest
使用方式1——源文件
按照上述方法每次編譯源文件
使用方式2——庫
將 fused-src/gtest/gtest-all.cc, fused-src/gtest/gtest.h 編譯成庫,每次自己寫 main 函數然後鏈接庫。
因此,在項目開發中,我們僅僅需要 fused-src/gtest/gtest-all.cc, fused-src/gtest/gtest.h 2 個文件即可。
運行過程
對於每個測試用例(TEST or TEST_F)都要經過如下流程:
1. 創建一個 test fixture(構造繼承自 testing:Test 的對象)
2. 運行 SetUp()
3. 運行測試用例(TEST or TEST_F)
4. 運行 TearDown()
5. 刪除 test fixture(析構繼承自 testing:Test 的對象)
因爲 TEST 沒有繼承 testing:Test 類("class GTEST_API_ Test", 抽象基類),所以具體表現是隻運行了第三步。
具體流程:
https://code.google.com/p/googletest/wiki/Primer