【IOS】在XCode4.2環境中配置gtest環境

1. 下載gtest安裝包,地址:http://code.google.com/p/googletest/downloads/list
2. 參考說明:http://code.google.com/p/googletest/wiki/V1_6_XcodeGuide
3. 詳細步驟及問題

3.1 編譯gtest.framework
A)根據readme,在安裝包目錄googletest-read-only/xcode/下打開工程文件gtest.xcodeproj
B)編譯gtest.framework


【Q1】根據說明,直接build,build失敗,報錯:The run destination My Mac 64-bit is not valid for Running the scheme 'gtest-framework'. The scheme 'gtest-framework' contains no buildables that can be built for the SDKs supported by the run destination My Mac 64-bit. Make sure your targets all specify SDKs that are supported by this version of Xcode.
【A1】
修改工程中的General.xcconfig
// Default SDK and minimum OS version is 10.4
SDKROOT = $(DEVELOPER_SDK_DIR)/MacOSX10.7.sdk
MACOSX_DEPLOYMENT_TARGET = 10.7
工程設置中修改SDK爲10.7, Compiler設置爲默認的編譯器;


【Q2】仍然編譯錯誤,提示有個變量未使用
【A2】選擇編譯器爲LLVM GCC 4.2


3.2 添加gtest.framework到工程中。
這裏有兩種方式可以實現這個動作:
A。把3.1編譯出來的gtest.framework添加到目標工程中,"Add->Existing Framework..." 或 "Project->Add..." ; gtest.framework中包含.h文件和Objc code。
B。把gtest.xcodeproj添加到目標工程中,作爲工程依賴關係。


3.3 開始Test Target
在XCode中創建一個"Shell Tool" target;
添加gtest.framework到target中,根據3.2,有兩種方式:
A. Add the gtest.framework to the "Link Binary with Libraries" build phase of your test target.
B. add gtest.framework to your "Link Binary with Libraries" build phase of your test target;

【Q3】"Shell Tool" target這種模式,在最新的XCode中已經沒有了,無法建立相應的target
【A3】這種方式無效。需要直接建立xcode工程去build。
用Command line tool替換,同樣可以實現。

3.4 build Target 
添加好gtest工程後,直接Build
【Q4】build失敗
【A4】設置工程的search header path,添加gtest的頭文件地址。


3.5 修改main文件
在main函數中執行如下代碼:
std::cout << "Running main() from gtest_main.cc\n";    
testing::InitGoogleTest(&argc, argv);


【Q5】main函數編譯錯誤
【A5】include gtest.h; 修改參數 const char * argv[]  ->  char * argv[]


3.6 編寫unit test方法
新建test.cpp, 內容如下:


TEST(SampleTest, Zero) {
    EXPECT_EQ(0, 0);
}
。。。。


3.8 執行Target
輸出結果如下:
Running main() from gtest_main.cc
[==========] Running 3 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 3 tests from SampleTest
[ RUN      ] SampleTest.Zero
[       OK ] SampleTest.Zero (0 ms)
[ RUN      ] SampleTest.Positive
[       OK ] SampleTest.Positive (0 ms)
[ RUN      ] SampleTest.Negative
[       OK ] SampleTest.Negative (0 ms)
[----------] 3 tests from SampleTest (0 ms total)


[----------] Global test environment tear-down
[==========] 3 tests from 1 test case ran. (0 ms total)
[  PASSED  ] 3 tests.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章