3. googletest单元测试框架

学习参考:玩转Google开源C++单元测试框架Google Test系列(gtest)(总)

googletest仓库地址

googletest是由测试技术团队根据Google的特定要求和约束开发的测试框架。无论您是在Linux,Windows还是Mac上工作,如果您编写C/C++代码,googletest都可以为您提供帮助。它支持任何类型的测试,而不仅仅是单元测试。

3.1 构建googletest

3.1.1 构建要求

这些是从源代码包构建和使用Google Test的基本要求:

  • Bazel或CMake。注意:Bazel是googletest在内部使用并对其进行测试的构建系统。CMake是社区支持的。
  • 一个符合C++ 11标准的编译器

3.1.2 构建googletest

  1. 克隆googletest

    twsa@twsa-VirtualBox:~/samba_share$ mkdir learn_gtest && cd learn_gtest
    twsa@twsa-VirtualBox:~/samba_share/learn_gtest$ git clone https://github.com/google/googletest
    
  2. 新建build目录。

如果已经进入了项目目录,比如下面,在项目目录下面新建一个build的目录,build目录存放编译googletest的输出包括gtest和gmock的静态库。

twsa@twsa-VirtualBox:~/samba_share/learn_gtest$ mkdir build
twsa@twsa-VirtualBox:~/samba_share/learn_gtest$ ls
build  googletest
  1. 编译gtest和gmock静态库
twsa@twsa-VirtualBox:~/samba_share/learn_gtest$ cd build/
twsa@twsa-VirtualBox:~/samba_share/learn_gtest$ cmake ../googletest/ && make

如果gcc默认的不是C++ 11需要添加支持C++ 11编译选项,具体是修改googletest的CMakeList.txt,修改具体过程如下

twsa@twsa-VirtualBox:~/samba_share/learn_gtest/googletest$ git diff CMakeLists.txt
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 37e7b61..0c53299 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -6,7 +6,7 @@ cmake_minimum_required(VERSION 2.8.8)
 if (POLICY CMP0048)
   cmake_policy(SET CMP0048 NEW)
 endif (POLICY CMP0048)
-
+add_definitions(-std=c++11)
 project(googletest-distribution)
 set(GOOGLETEST_VERSION 1.10.0)

静态库文件保存在build/lib目录下,如下:

twsa@twsa-VirtualBox:~/samba_share/learn_gtest/build/lib$ ls
libgmock.a  libgmock_main.a  libgtest.a  libgtest_main.a

3.2 第一个单元测试

3.2.1 定义函数接口

定义一个两个整数相加的函数接口

// src.h
int add(int a, int b);

3.2.2 编写测试代码

// test.cpp
#include "gtest/gtest.h"
extern "C" {
        #include "src.h"
}
/*
* 两个数相加之和
*/
TEST(src, add)
{
        int a = 1;
        int b = 2;
        ASSERT_EQ(3, add(1, 2));
}
int main(int argc, char* argv[])
{
        testing::InitGoogleTest(&argc, argv);
        return RUN_ALL_TESTS();
}

3.2.3 编写被测代码

// src.c
int add(int a, int b)
{
        return (a + b);
}

3.2.3 编写编译脚本

#! /bin/bash
build()
{
        gcc -c *.c
        g++  -c *.cpp -I ./googletest/googletest/include/  -std=c++11
        g++ *.o -o run -L build/lib -lgtest -lpthread
        ./run
}

clean()
{
        rm *.o
        rm run
}

if [[ -z "$1" ]]; then
        build
elif [[ $1 == clean ]]; then
        clean
fi

3.2.4 尝试让测试通过

./make.sh
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from src
[ RUN      ] src.add
[       OK ] src.add (0 ms)
[----------] 1 test from src (1 ms total)

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

测试通过,如果修改测试用例,使它测试不通过,修改如下

/*
* 两个数相加之和
*/
TEST(src, add)
{
        int a = 1;
        int b = 2;
        ASSERT_EQ(4, add(1, 2));
}

编译运行

./make.sh clean && ./make.sh
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from src
[ RUN      ] src.add
test.cpp:13: Failure
Expected equality of these values:
  4
  add(1, 2)
    Which is: 3
[  FAILED  ] src.add (1 ms)
[----------] 1 test from src (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (1 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] src.add

 1 FAILED TEST

测试不通过,通过这个报告可以看出定位错误,和分析错误的原因

results matching ""

    No results matching ""