简介

GoogleTest(简称 gtest),是 Google 开发的 C/C++单元测试框架。

GoogleTest 本身是用 C++编写的,主要用于测试 C++代码。要用其测试 C 语言代码,需要使用 extern "C"

通常是在 Linux 环境下使用 GoogleTest。如果要在 Windows 下使用,则需要MinGW或直接使用WSL

本文介绍了 Linux 下安装 GoogleTest 的方法,并给出了其测试 C 代码和 C++代码的例子。

GoogleTest 编译连接

GoogleTest 包含 3 个基础组件:

  • gtest/gtest.h
  • gtest 库
  • gtest_main 库

GoogleTest 对业务代码的测试是非侵入式的,即被测代码不需要做任何修改。单元测试时,会将被测试的业务代码、单元测试代码、GoogleTest 库编译连接成独立的可执行程序。

设想有一个项目,包含 calc.h,calc.cpp 和其他代码,那么业务代码和单元测试的关系如下:

描述 代码文件 业务可执行程序 单元测试可执行程序
业务代码主函数 main.cpp -
暂不测试的业务代码 other.cpp -
要测试的业务代码 calc.cpp
单元测试代码 calc_test.cpp -
GoogleTest 头文件 gtest/gtest.h -
GoogleTest 库 gtest -
GoogleTest 库 gtest_main -

通常,单元测试代码和业务代码一起进入版本控制系统。

Linux 下安装 GoogleTest

CentOS

1
# sudo yum install -y gtest

检查:

1
2
3
4
5
6
# whereis gtest
gtest: /usr/include/gtest
# whereis libgtest
libgtest: /usr/lib64/libgtest.so
# whereis libgtest_main
libgtest_main: /usr/lib64/libgtest_main.so

Ubuntu

1
# sudo apt-get install -y libgtest-dev

检查:

1
2
3
4
5
6
# whereis gtest
gtest: /usr/include/gtest
# whereis libgtest
libgtest: /usr/lib/x86_64-linux-gnu/libgtest.a /usr/local/lib/libgtest.a
# whereis libgtest_main
libgtest_main: /usr/lib/x86_64-linux-gnu/libgtest_main.a /usr/local/lib/libgtest_main.a

用 GoogleTest 测试 C++代码

如果要测试这样的代码:

1
2
3
4
5
6
7
8
// calc.hpp
#pragma once

class Calc {
public:
    int add(int a, int b);
};

1
2
3
4
5
6
// calc.cpp
#include "calc.hpp"

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

单元测试就要这样写:

1
2
3
4
5
6
7
8
9
// calc_test.cpp
#include <gtest/gtest.h>
#include "calc.hpp"

TEST(calc, add) {
    ASSERT_EQ(7, Calc().add(3, 4));
    ASSERT_EQ(150, Calc().add(50, 100));
}

然后编译连接

1
2
3
# g++ -c calc.cpp -o calc.o
# g++ -c calc_test.cpp -o calc_test.o
# g++ calc.o calc_test.o -lgtest -lgtest_main -pthread -o thetest

执行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# ./thetest
Running main() from /build/googletest-j5yxiC/googletest-1.10.0/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from calc
[ RUN      ] calc.add
[       OK ] calc.add (0 ms)
[----------] 1 test from calc (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 1 test.

用 GoogleTest 测试 C 代码

由于 GoogleTest 本身是 C++的,要测试 C 代码就要使用 extern "C"

如果要测试这样的代码:

1
2
3
4
5
// calc.h
#pragma once

int add(int a, int b);

1
2
3
4
5
6
// calc.c
#include "calc.h"

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

这时需要在 calc_test.cpp 里,用 extern "C" 包裹 #include "calc.h"

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// calc_test.cpp
#include <gtest/gtest.h>
extern "C" { // 在这里包裹
#include "calc.h"
}

TEST(calc, add) {
    ASSERT_EQ(7, add(3, 4));
    ASSERT_EQ(150, add(50, 100));
}

编译连接:

1
2
3
# gcc -c calc.c -o calc.o
# g++ -c calc_test.cpp -o calc_test.o
# g++ calc.o calc_test.o -lgtest -lgtest_main -pthread -o thetest

执行:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# ./thetest
Running main() from /build/googletest-j5yxiC/googletest-1.10.0/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from calc
[ RUN      ] calc.add
[       OK ] calc.add (0 ms)
[----------] 1 test from calc (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 1 test.

总结

本文介绍了 GoogleTest 在 Linux 安装和使用方法。

本文提到的代码可在seedjyh/googletest-example取得。

在实际项目中使用 GoogleTest 的方法,可以参阅下面这本书:

  • 修改代码的艺术:将大型、难懂、难修改的遗留代码^[遗留代码(Legacy code):指的是没有单元测试的代码]逐步改进为高内聚、低耦合、易测试的良好代码基的一整套步骤。