Xcode集成Google Test

时间:2021-10-02 09:21:50

Xcode集成Google Test

1.下载源代码https://github.com/google/googletest

2.进入下载文件目录下googletest/make中运行make gtest.a gtest_main.a

3.将gtest.a和gtest_main.a这两个文件放入至/usr/local/lib中,将googletest/include中的gtest文件夹放入至/usr/local/include中

4.在需要用到Google Test的工程中Build SettingsHeader Search Path填入/usr/local/includeLibrary Search Path填入/usr/local/libOther Linker Flags填入/usr/local/lib/gtest.a

5.测试,注意Xcode生成的main函数int main(int argc, const char ** argv)中的第二个参数前的const要去掉,代码如下:

#include "gtest/gtest.h"

int Factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
} return result;
} TEST(FactorialTest, Negative) {
// This test is named "Negative", and belongs to the "FactorialTest"
// test case.
EXPECT_EQ(1, Factorial(-5));
EXPECT_EQ(1, Factorial(-1));
EXPECT_GT(Factorial(-10), 0);
} int main(int argc,char ** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

参考

gtest.h file not found googletest xcode 7.0

Xcode 5集成GoogleTest Frame