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