天天看點

【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.