记录如何在Linux环境中使用gtest进行单元测试。

1 环境&工具说明

环境工具版本说明:

  1. Linux系统版本:
  2. GCC版本:7.5.0
  3. G++版本:7.5.0
  4. CMake版本:3.10.2
  5. gtest版本:1.12.1

2 安装工具

2.1 安装cmake

1
2
sudo apt update
sudo apt install build-essential cmake

2.2 安装gtest

2.2.1 下载gtest

gtest下载链接:gtest-1.12.1

2.2.2 安装gtest

1
2
3
4
5
6
7
mkdir build
cd build
cmake .. -DCMAKE_CXX_STANDARD=11  # 指定C++标准
make

# 安装到系统目录(可选,需sudo权限)
sudo make install

3 代码框架说明

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#.
#├── CMakeLists.txt
#└── src
#    ├── includ
#    │   └── add.h
#    ├── add.c
#    ├── test
#        ├── CMakeLists.txt
#        ├── test_main.cpp
#        └── test_add.cpp
#

3.1 add.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include "add.h"
  
static int counter = 0;

int add(int a, int b) {
    counter++;
    return a + b;
}

void reset_counter() {
    counter = 0;
}

int get_counter() {
    return counter;
}

3.2 add.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// include/add.h
#ifndef ADD_H
#define ADD_H

#ifdef __cplusplus
extern "C" {  // 确保C++编译器正确处理C函数
#endif

int add(int a, int b);
void reset_counter();
int get_counter();

#ifdef __cplusplus
}
#endif

#endif

3.3 test_main.cpp

1
2
3
4
5
6
#include <gtest/gtest.h>
  
int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

3.4 test_add.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <gtest/gtest.h>
#include "add.h"  // 包含C头文件

// 测试 add 函数
TEST(CCodeTest, AddFunction) {
    EXPECT_EQ(add(2, 3), 5);  // 调用C函数
    EXPECT_EQ(add(-1, 1), 0);
}

// 测试计数器逻辑
TEST(CCodeTest, CounterLogic) {
    reset_counter();  // 调用C函数重置计数器
    ASSERT_EQ(get_counter(), 0);

    add(1, 2);  // 调用C函数
    EXPECT_EQ(get_counter(), 1);
}

4 编写cmake

4.1 根目录下的CMakeLists.txt

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
cmake_minimum_required(VERSION 3.10)
project(CProjectWithGTest)

# 设置C标准
set(CMAKE_C_STANDARD 11)

# 添加C源代码为静态库
add_library(add STATIC
  src/add.c
)

# 包含头文件目录
target_include_directories(add PUBLIC src/include)

# 包含Google Test
find_package(GTest REQUIRED)
enable_testing()

# 添加测试子目录
add_subdirectory(src/test)

4.2 test目录下的CMakeList.txt

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
cmake_minimum_required(VERSION 3.10)
  
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})  # Var managed by CMake

message(STATUS "C Compiler: ${CMAKE_C_COMPILER}")
message(STATUS "C++ Compiler: ${CMAKE_CXX_COMPILER}")

# 测试代码使用C++编译器
add_executable(test_add
  test_main.cpp
  test_add.cpp
)

set(PROG_ROOT "/opt/code")
target_include_directories(test_add PRIVATE ${PROG_ROOT}/include)

# 链接C库和GTest库
target_link_libraries(test_add
  add
  ${GTEST_LIBRARIES}
  pthread
)

5 编译代码

1
2
3
4
5
6
7
8
9
# 生成makefile
cd /opt/code # 假设该目录为src所在目录
rm -rf build
mkdir build
cd build
cmake ..

# 编译代码,生成测试二进制文件
make

6 运行测试用例代码

若步骤5执行成功,则会在目录/opt/code/build/src/test下生成二进制文件test_add

1
2
cd /opt/code/build/src/test
./test_add

运行结果如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# [==========] Running 2 tests from 1 test suite.
# [----------] Global test environment set-up.
# [----------] 2 tests from CCodeTest
# [ RUN      ] CCodeTest.AddFunction
# [       OK ] CCodeTest.AddFunction (0 ms)
# [ RUN      ] CCodeTest.CounterLogic
# [       OK ] CCodeTest.CounterLogic (0 ms)
# [----------] 2 tests from CCodeTest (2 ms total)
# 
# [----------] Global test environment tear-down
# [==========] 2 tests from 1 test suite ran. (5 ms total)
# [  PASSED  ] 2 tests.