【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 
View Code

截图展示

cmake -DCMAKE_CXX_STANDARD=17 ..
View Code

 

make
View Code

 

make install 
View Code

 


检查是否成功安装好

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

make
View Code

sudo make install
View Code

 


测试

创建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})
CMakeList.txt

 

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();
}
Main.cpp

 

成功运行结果

 

 

 

 

 

posted @ 2023-11-26 22:40  Osea  阅读(74)  评论(0编辑  收藏  举报