How to install Gtest On MacOS and Xcode(如何在MacOS上安装gtest以及XCode使用gtest)

风格不统一 提交于 2020-04-26 15:33:36

How to install Gtest On MacOS

git clone https://github.com/google/googletest.git 
cd googletest
makedir build && cd build
cmake -DCMAKE_CXX_STANDARD=17 ..
make
sudo make install


######test
mkdir gtestDemo
vi CMakeList.txt
echo “export CPLUS_INCLUDE_PATH=/usr/local/include" >> ~/.zshrc  

echo "export LIBRARY_PATH=/usr/local/lib" >> ~/.zshrc  
source ~/.zshrc #tell terminal there are new variables  
#CMakeList.txt
cmake_minimum_required(VERSION 3.0)
set(CMAKE_CXX_STANDARD 17)
project(demo)
find_package(GTEST REQUIRED)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${GTEST_LIBRARIES})
#main.cpp
#include <iostream>
#include <gtest/gtest.h>
int add(int a, int b) {
    return a + b;
}
int sub(int a, int b) {
    return a - b;
}
// case1
TEST(test, c1) {
    EXPECT_EQ(3, add(1, 2));
    EXPECT_EQ(12, add(2, 6));
}
// case2
TEST(test, c2) {
    EXPECT_EQ(-1, sub(1, 2));
}
GTEST_API_ int main(int argc, char ** argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

cmake .
make
./demo
#you can also use the g++ command
g++ -std=c++11 -stdlib=libc++ main.cpp -o main

Xcode 中的使用

click the workspace and set like this, but the path you should replace by yourself.(if you install follow the step,you could not change it.)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


Conference

http://hack.limbicmedia.ca/installing-google-test/
https://blog.csdn.net/v55538679/article/details/77824214

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!