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
#include
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.)
http://hack.limbicmedia.ca/installing-google-test/
https://blog.csdn.net/v55538679/article/details/77824214