1 GoogleTest源码编译:
GoogleTest代码仓库URL:
https://github.com/google/googletest.git
下载源代码:
git clone --branch release-1.12.1 https://github.com/google/googletest.git googletest
1.1 Windows下GoogleTest的编译方法(包含example):
这里选择的编译器是Visual Studio 16 2019,需要用别的版本的编译器,请自行重新指定一下编译器版本:
1.1.1 debug版本编译:
mkdir debug # 在源码根目录创建一个名叫debug的文件夹
cd debug # 进入debug文件夹
cmake "../" -DCMAKE_CONFIGURATION_TYPES=Debug -Dgtest_force_shared_crt=ON -Dgtest_build_samples=ON -DCMAKE_INSTALL_PREFIX=D:/SDK/GoogleTest/v1.10.x/debug -G "Visual Studio 16 2019" -A x64
1.1.2 release版本编译:
mkdir release # 在源码根目录创建一个名叫release的文件夹
cd release # 进入release文件夹
cmake "../" -DCMAKE_CONFIGURATION_TYPES=Release -Dgtest_force_shared_crt=ON -Dgtest_build_samples=ON -DCMAKE_INSTALL_PREFIX=D:/SDK/GoogleTest/v1.10.x/release -G "Visual Studio 16 2019" -A x64
生成install文件:
管理员启动:x64_x86 Cross Tools Command Prompt for VS 2019.lnk
然后执行:msbuild INSTALL.vcxproj (release版本可能会报错)
或者使用用:cmake --build . --target INSTALL --config Release 编译并安装。
1.2 Linux下GoogleTest的编译方法(包含example):
1.2.1 debug版本编译:
mkdir debug # 在源码根目录创建一个名叫debug的文件夹
cd debug # 进入debug文件夹
cmake "../" \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_DEBUG_POSTFIX=d \
-Dgtest_force_shared_crt=ON \
-Dgtest_build_samples=ON \
-DCMAKE_INSTALL_PREFIX=~/SDK/googletest/v1.12.1/debug \
-DCMAKE_C_COMPILER=/usr/bin/gcc-11 \
-DCMAKE_CXX_COMPILER=/usr/bin/g++-11
make
make install
1.2.2 release版本编译:
mkdir release # 在源码根目录创建一个名叫release的文件夹
cd release # 进入release文件夹
cmake "../" \
-DCMAKE_BUILD_TYPE=Release \
-Dgtest_force_shared_crt=ON \
-Dgtest_build_samples=ON \
-DCMAKE_INSTALL_PREFIX=~/SDK/googletest/v1.12.1/release \
-DCMAKE_C_COMPILER=/usr/bin/gcc-11 \
-DCMAKE_CXX_COMPILER=/usr/bin/g++-11
编译参数含义说明:
2 GoogleTest常用测试宏:
3 GoogleTest使用步骤
3.1 以sample1为例:
sample1源码地址:https://github.com/calm2012/my_sample_code/tree/main/GoogleTest/GoogleTest_sample1
3.1.1 sample1目录结构如下:
└─sample1
│ CMakeLists.txt
│ sample1.h
│ sample1.cc
│ sample1_unittest.cc
│
├─debug
└─googletest_lib
CMakeLists.txt文件内容如下:
cmake_minimum_required(VERSION 3.14)
project(sample1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(sample1
sample1.h
sample1.cc
sample1_unittest.cc
)
# 链接gtest_maind.lib库
target_link_libraries(sample1 ${CMAKE_SOURCE_DIR}/googletest_lib/lib/debug/gtest_maind.lib)
# 链接gtestd.lib库
target_link_libraries(sample1 ${CMAKE_SOURCE_DIR}/googletest_lib/lib/debug/gtestd.lib)
# 在工程中共引入GoogleTest头文件搜索路径
target_include_directories(sample1 PUBLIC ${CMAKE_SOURCE_DIR}/googletest_lib/include)
sample1.h文件内容如下:
#ifndef GTEST_SAMPLES_SAMPLE1_H_
#define GTEST_SAMPLES_SAMPLE1_H_
// Returns n! (the factorial of n). For negative n, n! is defined to be 1.
int Factorial(int n);
#endif // GTEST_SAMPLES_SAMPLE1_H_
sample1.cc文件内容如下:
#include "sample1.h"
// Returns n! (the factorial of n). For negative n, n! is defined to be 1.
int Factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
sample1_unittest.cc文件内容如下:
#include <limits.h>
#include "sample1.h"
#include "gtest/gtest.h"
namespace {
// Tests factorial of negative numbers.
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);
}
// Tests factorial of 0.
TEST(FactorialTest, Zero) {
EXPECT_EQ(1, Factorial(0));
}
// Tests factorial of positive numbers.
TEST(FactorialTest, Positive) {
EXPECT_EQ(1, Factorial(1));
EXPECT_EQ(2, Factorial(2));
EXPECT_EQ(6, Factorial(3));
EXPECT_EQ(40320, Factorial(8));
}
} // namespace
3.1.2编译构建命令:
mkdir debug
cd debug
cmake "../" \
-DCMAKE_BUILD_TYPE:STRING=Debug \
-DCMAKE_C_COMPILER:STRING=/usr/bin/gcc \
-DCMAKE_CXX_COMPILER:STRING=/usr/bin/g++
cmake --build . --config Debug
cd Debug
运行:sample1
3.2 再看在MessagePush工程中如何引入GoogleTest
3.2.1 最后在你的工程里如CMakeLists.txt中引入:
add_gtest(base_common_unittest)
# 或者:
add_gtest_main(mysql_wrapper_unittest)
- add_gtest的作用是为了在Windows下添加gtest.lib,或者Linux下添加libgtest.a。同时引入GoogleTest头文件搜索路径
- add_gtest_main的作用是为了在Windows下添加gtest.lib、gtest_main.lib,或者Linux下添加libgtest.a、libgtest_main.a。同时引入GoogleTest头文件搜索路径
add_gtest宏:
macro(add_gtest target_name)
target_link_libraries(${target_name}
${googletest_lib_path}/${gtes_lib_name} # googletest_lib_path: gooltest库文件路径
${target_name} ${googletest_lib_path}/${gmock_lib_name}
-lpthread
)
target_include_directories(${target_name} PUBLIC ${googletest_include_path})
endmacro(add_gtest)
add_gtest_main宏:
macro(add_gtest_main target_name)
target_link_libraries(${target_name}
${googletest_lib_path}/${gtes_lib_name} # googletest_lib_path: gooltest库文件路径
${googletest_lib_path}/${gtes_main_lib_name}
${googletest_lib_path}/${gmock_lib_name}
${googletest_lib_path}/${gmock_main_lib_name}
-lpthread
)
target_include_directories(${target_name} PUBLIC ${googletest_include_path})
endmacro(add_gtest_main)
3.3 GoogleTest常用测试宏(TEST/TEST_F/TEST_P/TYPED_TEST)的一般使用场景:
- TEST:通常用于简单的函数测试。
- TEST_F:通常用于需要访问类对象时的测试。
- TEST_P:通常用于把测试对象当作参数传入或有大量重复测试case时。
- TYPED_TEST:通常用于接口测试。
3.4 其它参考网址:
- googletest:https://github.com/google/googletest
- googletest-gmock使用示例:https://zhuanlan.zhihu.com/p/101906555
- GoogleTest单元测试学习:https://blog.csdn.net/Tosonw/article/details/89449285
- GoogleTest Primer:https://blog.csdn.net/qq_34548075/article/details/122864792