(自用)gtest单元测试

gtest是Google的一套用于编写C++测试的框架,可以运行在很多平台上(包括Linux、Mac OS X、Windows、Cygwin等等)。基于xUnit架构。支持很多好用的特性,包括自动识别测试、丰富的断言、断言自定义、死亡测试、非终止的失败、生成XML报告等等。

1.gtest优点

2.搭建测试框架

gtest下载地址: GitHub - google/googletest: GoogleTest - Google Testing and Mocking Framework

下载方法是:git clone GitHub - google/googletest: GoogleTest - Google Testing and Mocking Framework

安装方法是:

$ cd googletest

注意:如果在make 过程中报错,可在CMakeLists.txt 中增加如下行,再执行下面的命令:  SET(CMAKE_CXX_FLAGS "-std=c++11")  

$ cmake .    

$ make

然后在lib目录下会生成:libgmock.a libgmock_main.a libgtest.a libgtest_main.a

最后我们再sudo make install。

测试demo

目录结构:

要测试的两个函数如下:

simple1.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;
}

// Returns true iff n is a prime number.
bool IsPrime(int n) {
  // Trivial case 1: small numbers
  if (n <= 1) return false;

  // Trivial case 2: even numbers
  if (n % 2 == 0) return n == 2;

  // Now, we have that n is odd and n >= 3.

  // Try to divide n by every odd number i, starting from 3
  for (int i = 3; ; i += 2) {
    // We only have to try i up to the square root of n
    if (i > n/i) break;

    // Now, we have i <= n/i < n.
    // If n is divisible by i, n is not prime.
    if (n % i == 0) return false;
  }

  // n has no integer factor in the range (1, n), and thus is prime.
  return true;
}

头文件sample.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);

// Returns true iff n is a prime number.
bool IsPrime(int n);

#endif 
编写测试类

1.包含头文件gtest/gtest.h、limits.h(最大int和最小int)

2.格式

TEST(测试套名,测试用例名)
{
    //EXPECT_XX

}

一个测试套下可以有多个测试用例

eg:

simple_unittest.cc

#include <limits.h>//包含最大最小数
#include "sample1.h"
#include "gtest/gtest.h"//gtest头文件
namespace {

    TEST(FactorialTest, Negative) {
        // 测试名叫Negative,属于FactorialTest测试套
        // 测试用例
        EXPECT_EQ(1, Factorial(-5));//断言相等
        EXPECT_EQ(1, Factorial(-1));
        //前后顺序不影响
        EXPECT_GT(Factorial(-10), 0);//断言大于
    }

    TEST(FactorialTest, Zero) {
        EXPECT_EQ(1, Factorial(0));
    }

    TEST(FactorialTest, Positive) {
        EXPECT_EQ(1, Factorial(1));
        EXPECT_EQ(2, Factorial(2));
        EXPECT_EQ(6, Factorial(3));
        EXPECT_EQ(40320, Factorial(8));
    }

    // Tests IsPrime() 测试是否是素数
    TEST(IsPrimeTest, Negative) {
        //预测 true or false
        EXPECT_FALSE(IsPrime(-1));
        EXPECT_FALSE(IsPrime(-2));
        EXPECT_FALSE(IsPrime(INT_MIN));
    }

    TEST(IsPrimeTest, Trivial) {
        EXPECT_FALSE(IsPrime(0));
        EXPECT_FALSE(IsPrime(1));
        EXPECT_TRUE(IsPrime(2));
        EXPECT_TRUE(IsPrime(3));
    }

    TEST(IsPrimeTest, Positive) {
        EXPECT_FALSE(IsPrime(4));
        EXPECT_TRUE(IsPrime(5));
        EXPECT_FALSE(IsPrime(6));
        EXPECT_TRUE(IsPrime(23));
    }
}  // namespace

进行测试
不带main

 实现测试的main函数,当然我们也可以不用写main函数,那就需要连接gtest_main.a这个库(编译时添加-lgtest_main选项)。比如这样子编译:

g++ sample1.cc sample1_unittest.cc -lgtest -std=c++14 -lgtest_main -lpthread -o test1

执行结果:

带main

固定写法,将main补充在最后面

int main(int argc, char** argv)
{
    testing::InitGoogleTest(&argc,argv);
    return RUN_ALL_TESTS();
}

完整测试.cc:

#include <limits.h>//包含最大最小数
#include "sample1.h"
#include "gtest/gtest.h"//gtest头文件
namespace {

    TEST(FactorialTest, Negative) {
        // 测试名叫Negative,属于FactorialTest测试套
        // 测试用例
        EXPECT_EQ(1, Factorial(-5));//断言相等
        EXPECT_EQ(1, Factorial(-1));
        //前后顺序不影响
        EXPECT_GT(Factorial(-10), 0);//断言大于
    }

    TEST(FactorialTest, Zero) {
        EXPECT_EQ(1, Factorial(0));
    }

    TEST(FactorialTest, Positive) {
        EXPECT_EQ(1, Factorial(1));
        EXPECT_EQ(2, Factorial(2));
        EXPECT_EQ(6, Factorial(3));
        EXPECT_EQ(40320, Factorial(8));
    }

    // Tests IsPrime() 测试是否是素数
    TEST(IsPrimeTest, Negative) {
        //预测 true or false
        EXPECT_FALSE(IsPrime(-1));
        EXPECT_FALSE(IsPrime(-2));
        EXPECT_FALSE(IsPrime(INT_MIN));
    }

    TEST(IsPrimeTest, Trivial) {
        EXPECT_FALSE(IsPrime(0));
        EXPECT_FALSE(IsPrime(1));
        EXPECT_TRUE(IsPrime(2));
        EXPECT_TRUE(IsPrime(3));
    }

    TEST(IsPrimeTest, Positive) {
        EXPECT_FALSE(IsPrime(4));
        EXPECT_TRUE(IsPrime(5));
        EXPECT_FALSE(IsPrime(6));
        EXPECT_TRUE(IsPrime(23));
    }
}  // namespace

int main(int argc, char** argv)
{
    testing::InitGoogleTest(&argc,argv);
    return RUN_ALL_TESTS();
}

编译时去掉-lgtest_main选项:

相关推荐

  1. gtest 单元测试

    2024-07-11 03:16:08       51 阅读
  2. 【C++】Google Test(gtest单元测试

    2024-07-11 03:16:08       23 阅读
  3. 【C++】使用gtest单元测试框架写单元测试

    2024-07-11 03:16:08       18 阅读
  4. 软件测试:C++ Google Test单元测试框架GTest

    2024-07-11 03:16:08       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 03:16:08       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 03:16:08       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 03:16:08       58 阅读
  4. Python语言-面向对象

    2024-07-11 03:16:08       69 阅读

热门阅读

  1. Redis 数据过期及淘汰策略

    2024-07-11 03:16:08       20 阅读
  2. VSCode 推荐插件列表(都安装到Remote SSH上)

    2024-07-11 03:16:08       18 阅读
  3. bug——多重定义

    2024-07-11 03:16:08       23 阅读
  4. Tkinter 部件使用教程

    2024-07-11 03:16:08       20 阅读
  5. ASPICE评估是汽车软件质量的可靠保障

    2024-07-11 03:16:08       21 阅读
  6. AI绘画好学吗?解锁创意无限的艺术新纪元

    2024-07-11 03:16:08       24 阅读
  7. P1255 数楼梯【递推+大数】

    2024-07-11 03:16:08       20 阅读
  8. 中断相关知识

    2024-07-11 03:16:08       21 阅读
  9. 春风得意特斯拉(六)

    2024-07-11 03:16:08       22 阅读
  10. C语言10 函数

    2024-07-11 03:16:08       21 阅读
  11. 在Qt中使用C++编程与传统C++编程的区别

    2024-07-11 03:16:08       17 阅读
  12. 【Android】【多屏】多屏异显异触调试技巧总结

    2024-07-11 03:16:08       23 阅读