【GoogleTest】MacOS & Linux 环境下安装测试库
参考博客
MacOS
Linux
代码仓库
安装展示
MacOS
命令行输入
git clone http://github.com/google/googletest cd googletest mkdir build && cd build cmake -DCMAKE_CXX_STANDARD=17 .. make make install
截图展示
cmake -DCMAKE_CXX_STANDARD=17 ..
make
make install
检查是否成功安装好
ls /usr/local/lib/pkgconfig/*test* # 如果成功下载后,就会在 /usr/local/lib/pkgconfig/下出现 test
Linux
命令行
git clone https://github.com/google/googletest.git cd googletest mkdir build cd build cmake .. make sudo make install
cmake ..
make
sudo make install
测试
创建Demo —— CLion项目
CMakeList.txt文件
cmake_minimum_required(VERSION 3.0) set(CMAKE_CXX_STANDARD 11) project(demo) find_package(GTEST REQUIRED) add_executable(${PROJECT_NAME} test/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(6, 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(); }
成功运行结果