GoogleTest環境配置以及應用

  • 2022 年 8 月 20 日
  • 筆記

1 GoogleTest源碼編譯:

GoogleTest代碼倉庫URL:
//github.com/google/googletest.git
下載源代碼:

git clone --branch release-1.12.1 //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源碼地址://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 其它參考網址: