2

GoogleTest环境配置以及应用 - 放逐²º¹²

 2 years ago
source link: https://www.cnblogs.com/calm2012/p/16608122.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

更多文章:https://github.com/calm2012

个人博客:https://calm2012.github.io

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,需要用别的版本的编译器,请自行重新指定一下编译器版本:

VS2022为:"Visual Studio 17 2022"
VS2019为:"Visual Studio 16 2019"
VS2017为:"Visual Studio 15 2017"
VS2015为:"Visual Studio 14 2015"
VS2013为:"Visual Studio 12 2013"
VS2012为:"Visual Studio 11 2012"
VS2010为:"Visual Studio 10 2010"

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

编译参数含义说明:

-DCMAKE_BUILD_TYPE:用来制定编译Debug版本,还是Release版本。
-DCMAKE_DEBUG_POSTFIX:debug版本生成lib追加的后缀名(d表示原本为xx.so,编译生成的是xxd.so)
-Dgtest_force_shared_crt=ON:Winodws设置构建库类型为MD。
-DCMAKE_INSTALL_PREFIX=~/SDK/googletest/v1.12.1/debug :指定SDK生成后的保存目录。
-DCMAKE_C_COMPILER=/usr/bin/gcc-11:指定使用的gcc编译版本。
-DCMAKE_CXX_COMPILER=/usr/bin/g++-11:指定使用的g++版本。


2 GoogleTest常用测试宏:

ASSERT宏 EXPECT宏 功能 使用场景
ASSERT_TRUE EXPECT_TRUE 判真 判断表达式真假
ASSERT_FALSE EXPECT_FALSE 判假 判断表达式真假
ASSERT_EQ EXPECT_EQ 相等 数值比较
ASSERT_NE EXPECT_NE 不等 数值比较
ASSERT_GT EXPECT_GT 大于 数值比较
ASSERT_LT EXPECT_LT 小于 数值比较
ASSERT_GE EXPECT_GE 大于或等于 数值比较
ASSERT_LE EXPECT_LE 小于或等于 数值比较
ASSERT_FLOAT_EQ EXPECT_FLOAT_EQ 单精度浮点值相等 数值比较
ASSERT_DOUBLE_EQ EXPECT_DOUBLE_EQ 双精度浮点值相等 数值比较
ASSERT_NEAR EXPECT_NEAR 浮点值接近(第3个参数为误差阈值) 双精度浮点值相等、数值比价
ASSERT_STREQ EXPECT_STREQ C字符串相等 字符串比较
ASSERT_STRNE EXPECT_STRNE C字符串不等 字符串比较
ASSERT_STRCASEEQ EXPECT_STRCASEEQ C字符串相等(忽略大小写) 字符串比较
ASSERT_STRCASENE EXPECT_STRCASENE C字符串不等(忽略大小写) 字符串比较
ASSERT_PRED1 EXPECT_PRED1 自定义谓词函数,(pred, arg1)(还有_PRED2, ..., _PRED5 自定义比较函数

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 其它参考网址:



About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK