宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

gtest初识总结

本文以结合gtest github内容进行学习gtest。

gtest github地址

gtest编译

g++ xx.cpp xx.h -lgtest -lpthread -o main

gtest编写

创建测试的一个简易的步骤:

1.使用TEST)宏来定义和命名测试函数,这些是不返回值的普通C ++函数。
2.在此函数中,与要包含的任何有效C ++语句一起使用各种googletest断言来检查值.ASSERT_)、EXPECT_))
3.测试的结果由断言决定; 如果测试中的任何断言失败(无论是致命的还是非致命的),或者测试崩溃,整个测试都会失败。否则,它会成功。

TEST)第一个参数是测试用例的名称,第二个参数是测试用例中的测试名称(有效的C++标识符,不应包含下划线)。
googletest按照测试用例对测试结果进行分组。

例子Demo

文件 描述
sample1.cc 待测试代码包含两个函数:
1:Factorialint n) 阶乘函数。
2:IsPrimeint n) 是否是质数
sample1.h 待测试代码头文件
simple1_unittest.cc 测试用例代码文件
main.cpp 程序入口文件

代码如下:

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 Factorialint n);

// Returns true iff n is a prime number.
bool IsPrimeint n);

#endif  // GTEST_SAMPLES_SAMPLE1_H_

sample.cc

#include "sample1.h"

// Returns n! the factorial of n).  For negative n, n! is defined to be 1.
int Factorialint n) {
  int result = 1;
  for int i = 1; i <= n; i++) {
    result *= i;
  }

  return result;
}

// Returns true iff n is a prime number.
bool IsPrimeint 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;
}

sample1_unittest.cc

#include <limits.h>
#include "sample1.h"
#include "gtest/gtest.h"
namespace {

// Tests Factorial).

// Tests factorial of negative numbers.
TESTFactorialTest, Negative) {
  // This test is named "Negative", and belongs to the "FactorialTest"
  // test case.
  EXPECT_EQ1, Factorial-5)) << "this sunrise test"; //后面的信息在失败的情况下输出到终端
  EXPECT_EQ1, Factorial-1));
  EXPECT_GTFactorial-10), 0);

// Tests factorial of 0.
TESTFactorialTest, Zero) {
  EXPECT_EQ1, Factorial0));
}

// Tests factorial of positive numbers.
TESTFactorialTest, Positive) {
  EXPECT_EQ1, Factorial1));
  EXPECT_EQ2, Factorial2));
  EXPECT_EQ6, Factorial3));
  EXPECT_EQ40320, Factorial8));
}


// Tests IsPrime)

// Tests negative input.
TESTIsPrimeTest, Negative) {
  // This test belongs to the IsPrimeTest test case.

  EXPECT_FALSEIsPrime-1));
  EXPECT_FALSEIsPrime-2));
  EXPECT_FALSEIsPrimeINT_MIN));
}

// Tests some trivial cases.
TESTIsPrimeTest, Trivial) {
  EXPECT_FALSEIsPrime0));
  EXPECT_FALSEIsPrime1));
  EXPECT_TRUEIsPrime2));
  EXPECT_TRUEIsPrime3));
}

// Tests positive input.
TESTIsPrimeTest, Positive) {
  EXPECT_FALSEIsPrime4));
  EXPECT_TRUEIsPrime5));
  EXPECT_FALSEIsPrime6));
  EXPECT_TRUEIsPrime23));
}
} // namespace

main.cpp

#include<iostream>
#include<gtest/gtest.h>

using namespace std;

GTEST_API_ int mainint argc, char **argv) {
    printf"Running main");
    testing::InitGoogleTest&argc, argv);
    return RUN_ALL_TESTS);
}

编译命令:

g++ sample1.cc sample1.h sample1_unittest.cc main.cpp -lgtest -lpthread -o main

运行效果如下:

Running main[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN      ] FactorialTest.Negative
[       OK ] FactorialTest.Negative 0 ms)
[ RUN      ] FactorialTest.Zero
[       OK ] FactorialTest.Zero 0 ms)
[ RUN      ] FactorialTest.Positive
[       OK ] FactorialTest.Positive 0 ms)
[----------] 3 tests from FactorialTest 0 ms total)

[----------] 3 tests from IsPrimeTest
[ RUN      ] IsPrimeTest.Negative
[       OK ] IsPrimeTest.Negative 0 ms)
[ RUN      ] IsPrimeTest.Trivial
[       OK ] IsPrimeTest.Trivial 0 ms)
[ RUN      ] IsPrimeTest.Positive
[       OK ] IsPrimeTest.Positive 0 ms)
[----------] 3 tests from IsPrimeTest 0 ms total)

[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. 0 ms total)
[  PASSED  ] 6 tests.

更改FactorialTest.Negative中的用例代码

// EXPECT_EQ1, Factorial-5)) << "this sunrise test"; //后面的信息在失败的情况下输出到终端
EXPECT_EQ-1, Factorial-5)) << "this sunrise test";

运行效果:

Running main[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN      ] FactorialTest.Negative
sample1_unittest.cc:79: Failure
Value of: Factorial-5)
  Actual: 1
Expected: -1
this sunrise test
[  FAILED  ] FactorialTest.Negative 0 ms)
[ RUN      ] FactorialTest.Zero
[       OK ] FactorialTest.Zero 0 ms)
[ RUN      ] FactorialTest.Positive
[       OK ] FactorialTest.Positive 0 ms)
[----------] 3 tests from FactorialTest 0 ms total)

[----------] 3 tests from IsPrimeTest
[ RUN      ] IsPrimeTest.Negative
[       OK ] IsPrimeTest.Negative 0 ms)
[ RUN      ] IsPrimeTest.Trivial
[       OK ] IsPrimeTest.Trivial 0 ms)
[ RUN      ] IsPrimeTest.Positive
[       OK ] IsPrimeTest.Positive 0 ms)
[----------] 3 tests from IsPrimeTest 0 ms total)

[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. 0 ms total)
[  PASSED  ] 5 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] FactorialTest.Negative

 1 FAILED TEST

基础语法介绍

断言

分为ASSERT_*和EXPECT_*两种类型:

ASSERT_* EXPECT_*
致命的断言,终止当前功能以测试用例为组) 非致命故障,不会终止当前功能

终止:是终止自身处于的那一组测试用例,如上例中的FactorialTest.Negative是一组测试。

断言详细函数
  • 基本函数,基本的真/假条件测试。
Fatal assertion Nonfatal assertion Verifies
ASSERT_TRUEcondition); EXPECT_TRUEcondition); condition is true
ASSERT_FALSEcondition); EXPECT_FALSEcondition); condition is false
  • 二元比较
Fatal assertion Nonfatal assertion Verifies
ASSERT_EQval1, val2); EXPECT_EQval1, val2); val1 == val2
ASSERT_NEval1, val2); EXPECT_NEval1, val2); val1 != val2
ASSERT_LTval1, val2); EXPECT_LTval1, val2); val1 < val2
ASSERT_LEval1, val2); EXPECT_LEval1, val2); val1 <= val2
ASSERT_GTval1, val2); EXPECT_GTval1, val2); val1 > val2
ASSERT_GEval1, val2); EXPECT_GEval1, val2); val1 >= val2
  • 字符串比较
Fatal assertion Nonfatal assertion Verifies
ASSERT_STREQstr1, str2); EXPECT_STREQstr1, str2); the two C strings have the same content
ASSERT_STRNEstr1, str2); EXPECT_STRNEstr1, str2); the two C strings have different contents
ASSERT_STRCASEEQstr1, str2); EXPECT_STRCASEEQstr1, str2); the two C strings have the same content, ignoring case
ASSERT_STRCASENEstr1, str2); EXPECT_STRCASENEstr1, str2); the two C strings have different contents, ignoring case
Test Fixtures: 为多个测试使用相同的数据配置

Fixtures 是测试中非常重要的一部分。他们的主要目的是建立一个固定/已知的环境状态以确保 测试可重复并且按照预期方式运行。

  1. 创建Fixture类继承至::testing::Test.
  2. 在类中,声明需要使用的对象
  3. 编写SetUp函数
  4. 编写TearDown函数
  5. 如果需要,请为要共享的测试定义子例程。

例子:
Fixtures类

class QueueTest : public ::testing::Test {
 protected:
  void SetUp) override {
     q1_.Enqueue1);
     q2_.Enqueue2);
     q2_.Enqueue3);
  }

  // void TearDown) override {}

  Queue<int> q0_;
  Queue<int> q1_;
  Queue<int> q2_;
};

测试用例

TEST_FQueueTest, IsEmptyInitially) {
  EXPECT_EQq0_.size), 0);
}

TEST_FQueueTest, DequeueWorks) {
  // construct an instance QueueTest q;q.SetUp)
  int* n = q0_.Dequeue);
  EXPECT_EQn, nullptr);

  n = q1_.Dequeue);
  ASSERT_NEn, nullptr);
  EXPECT_EQ*n, 1);
  EXPECT_EQq1_.size), 0);
  delete n;

  n = q2_.Dequeue);
  ASSERT_NEn, nullptr);
  EXPECT_EQ*n, 2);
  EXPECT_EQq2_.size), 1);
  delete n;
  // q.TearDown)
}

用例DequeueWorks和用例DequeueWorks共用的QueueTest中的q0_,q1_,q2_,SetUp)和TearDown).