google test 是google的c++開源單元測試工具,chrome的開發團隊就是使用它。
Code::Blocks 12.11(MinGW 4.7.1) (Windows版)
Google Test 1.7.0
一 編譯Google Test
運行cmake-gui
在彈出來的對話框中
Where is the source code之后填寫解壓的gtest,例如:D:/software/gtest-1.7.0
Where to build the binaries之后填寫生成的工程路徑,例如:D:/software/gtest-1.7.0/codeblocks-mingw
(網上看到說要點擊Configure,然后勾選gtest_disable_pthreads之后的復選框,再進行下面的操作,這一步我沒有做也沒問題)
點擊Configure
然后點擊Generate
如此一來,cmake就在D:/software/gtest-1.7.0/codeblocks-mingw下會自動生成Code::Blocks的project文件。
雙擊該project文件(cbp文件),然后build就行了。
二 使用Google Test
需要注意的是:
既想用C++11的新特征,又想順利編譯Google Test,你得用命令行參數-std=gnu++11,而不是-std=c++11。
用codeblocks新建一個工程
右鍵project,選擇Build options...
清除Compiler settings > Compile Flags下與-std=有關的復選框
點擊選項卡Compiler settings > Other options,在對話框中填寫-std=gnu++11
(如果沒有使用c++11新特性,上面兩步可以省略)
在Linker settings > Other linker options下填寫-lgtest
點擊OK
點擊選項卡Search directories
在Compiler子選項卡中Add一項,填寫D:/software/gtest-1.7.0\include
在Linker子選項卡中Add一項,填寫D:/software/gtest-1.7.0\codeblocks-mingw
點擊OK
然后就可以寫代碼了,我寫了一個add函數,然后對其測試,代碼如下:
1 #include<cstdio> 2 #include<gtest/gtest.h> 3 4 int add(int a, int b) 5 { 6 return a+b; 7 } 8 9 TEST(addtest, HandleNoneZeroInput) 10 { 11 EXPECT_EQ(14, add(4, 10)); 12 EXPECT_EQ(-2, add(-3, 1)); 13 } 14 15 int main(int argc, char *argv[]) 16 { 17 testing::InitGoogleTest(&argc, argv); 18 return RUN_ALL_TESTS(); 19 return 0; 20 }
結果如圖:
詳細的使用說明請參考:here
【版權聲明】轉載請注明出處:http://www.cnblogs.com/TenosDoIt/p/3412721.html
