• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

单元测试gtest的安装和使用方法结合官网的sample

武飞扬头像
CAccept
帮助1

🍎什么是gtest

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

⭐gtest的优点

好的测试应该有下面的这些特点,我们看看gtest是如何满足要求的。

  • 测试应该是独立的可重复的。一个测试的结果不应该作为另一个测试的前提。gtest中每个测试运行在独立的对象中。如果某个测试失败了,可以单独地调试它。
  • 测试应该是有清晰的结构的。gtest的测试有很好的组织结构,易于维护。
  • 测试应该是可移植可复用的。有很多代码是不依赖平台的,因此它们的测试也需要不依赖于平台。gtest可以在多种操作系统、多种编译器下工作,有很好的可移植性。
  • 测试失败时,应该给出尽可能详尽的信息。gtest在遇到失败时并不停止接下来的测试,而且还可以选择使用非终止的失败来继续执行当前的测试。这样一次可以测试尽可能多的问题。
  • 测试框架应该避免让开发者维护测试框架相关的东西。gtest可以自动识别定义的全部测试,你不需要一个一个地列举它们。
  • 测试应该够快。gtest在满足测试独立的前提下,允许你复用共享数据,它们只需创建一次。
  • gtest采用的是xUnit架构,你会发现和JUnitPyUnit很类似,所以上手非常快。

⭐下载以及安装gtest

下载:git clone https://github.com/谷歌/谷歌test.git
1、$ cd 谷歌test
2、$ cmake .
3、$ make
注意:如果在make 过程中报错,可在CMakeLists.txt 中增加如下行,再执行下面的命令: SET(CMAKE_CXX_FLAGS "-std=c 11") ,重新执行cmake .以及make
然后在lib目录下会生成:libgmock.a libgmock_main.a libgtest.a libgtest_main.a
4、最后我们再sudo make install。
学新通

⭐gtest断言类型

ASSERT_*断言和EXPECT_*断言的区别:
当ASSERT断言失败时,退出当前TEST,但是可以继续执行其他TEST(在Google Test中,每个TEST都是相互独立的,这意味着一个测试的失败不会影响其他测试的执行。)
换句话说,ASSERT_类型的断言是致命的,如果它们失败,那么测试将会停止执行。这可以帮助你快速地定位错误,但同时也会影响到测试的覆盖范围。而EXPECT_类型的断言则是非致命的,即使它们失败,测试仍将继续执行,这可以让你获得更多的测试覆盖范围,但也可能导致测试过于宽松,因为它们无法确保测试的正确性。

因此,使用哪种类型的断言取决于你的具体需求。如果你想快速地定位错误并停止测试,那么使用ASSERT_;如果你更关心测试覆盖范围,并希望测试能够继续执行,那么使用EXPECT_。

ASSERT_XXX(val1,val2)中val1是期待值而val2是实际值(EXPECT_XXX同理),下面总结了一些ASSERT和EXPECT,但是还不完整可以在用到的时候自己再去查
学新通

⭐头文件和库

当我们make install 以后gtest相关头文件已经安装到/usr/local/include/下了,所以在使用的时候直接写#include "gtest/gtest.h"就可以了,而对于库文件,由于make install的时候已经将gtest的相关库安装到/usr/local/lib下了,所以后续使用的话直接链接就好了不用加路径

  • libgtest.a或libgtest.so:gtest库的静态或动态链接库文件,包含gtest的实现代码。
  • libgtest_main.a或libgtest_main.so:gtest库的静态或动态链接库文件,包含gtest的主函数实现和启动测试的代码(当我们的测试文件中没有main函数的话就可以链接到这个库就可以运行了)。

🎂gtest的使用【官网例子】

如何运行TEST程序:
1、写main方法,其中调用RUN_ALL_TESTS函数即可。

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

2、我们也可以不用写main函数,那就需要链接gtest_main.a这个静态库

⭐sample1

sample1.h

#ifndef GOOGLETEST_SAMPLES_SAMPLE1_H_
#define GOOGLETEST_SAMPLES_SAMPLE1_H_

// Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
int Factorial(int n);

// Returns true if and only if n is a prime number.
bool IsPrime(int n);

#endif  // GOOGLETEST_SAMPLES_SAMPLE1_H_

sample1.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 if and only if 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_unittest.cc

#include <limits.h>
#include "sample1.h"
//已经make install到了/usr/local/include/gtest/gtest.h
#include "gtest/gtest.h"
namespace {

//测试哪个函数以及这个测试的名字(也不一定要这样写,只是这样更加清楚)
TEST(FactorialTest, Negative) {
    // This test is named "Negative", and belongs to the "FactorialTest"
    // test case.
    // 断言,运行Factorial(-5)并对比结果是不是等于1(Factorial(-5)==1?)
    EXPECT_EQ(1, Factorial(-5));
    EXPECT_EQ(1, Factorial(-1));
    // 断言,运行Factorial(-10)并对比结果是不是小于0(0 > actorial(-10)?)
    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) {
    //预测是不是返回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
学新通

如何编译呢?👇
1、g sample1.cc sample1_unittest.cc -lgtest -std=c 14 -lgtest_main -lpthread -o test1
感兴趣的同学可以试试实现main方法来运行这个TEST文件
运行结果:
学新通
当我把第一个TEST进行改变
学新通

学新通
在这个sample里面用到EXPECT_FALSE、EXPECT_TRUE、EXPECT_GT、EXPECT_EQ
学新通
学新通


⭐sample2

sample2.h

#ifndef GOOGLETEST_SAMPLES_SAMPLE2_H_
#define GOOGLETEST_SAMPLES_SAMPLE2_H_

#include <string.h>

// A simple string class.
class MyString {
 private:
  const char* c_string_;
  const MyString& operator=(const MyString& rhs);

 public:
  // Clones a 0-terminated C string, allocating memory using new.
  static const char* CloneCString(const char* a_c_string);

  
  //
  // C'tors

  // The default c'tor constructs a NULL string.
  MyString() : c_string_(nullptr) {}

  // Constructs a MyString by cloning a 0-terminated C string.
  explicit MyString(const char* a_c_string) : c_string_(nullptr) {
    Set(a_c_string);
  }

  // Copy c'tor
  MyString(const MyString& string) : c_string_(nullptr) {
    Set(string.c_string_);
  }

  
  //
  // D'tor.  MyString is intended to be a final class, so the d'tor
  // doesn't need to be virtual.
  ~MyString() { delete[] c_string_; }

  // Gets the 0-terminated C string this MyString object represents.
  const char* c_string() const { return c_string_; }

  size_t Length() const { return c_string_ == nullptr ? 0 : strlen(c_string_); }

  // Sets the 0-terminated C string this MyString object represents.
  void Set(const char* c_string);
};

#endif  // GOOGLETEST_SAMPLES_SAMPLE2_H_
学新通

sample2.cc

#include "sample2.h"

#include <string.h>

// Clones a 0-terminated C string, allocating memory using new.
const char* MyString::CloneCString(const char* a_c_string) {
  if (a_c_string == nullptr) return nullptr;

  const size_t len = strlen(a_c_string);
  char* const clone = new char[len   1];
  memcpy(clone, a_c_string, len   1);

  return clone;
}

// Sets the 0-terminated C string this MyString object
// represents.
void MyString::Set(const char* a_c_string) {
  // Makes sure this works when c_string == c_string_
  const char* const temp = MyString::CloneCString(a_c_string);
  delete[] c_string_;
  c_string_ = temp;
}

学新通

sample2_unittest.cc

#include "sample2.h"

#include "gtest/gtest.h"
namespace {
// In this example, we test the MyString class (a simple string).

// Tests the default c'tor.
TEST(MyString, DefaultConstructor) {
  const MyString s;
  EXPECT_STREQ(nullptr, s.c_string());

  EXPECT_EQ(0u, s.Length());
}

const char kHelloString[] = "Hello, world!";

// Tests the c'tor that accepts a C string.
TEST(MyString, ConstructorFromCString) {
  const MyString s(kHelloString);
  EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
  EXPECT_EQ(sizeof(kHelloString) / sizeof(kHelloString[0]) - 1, s.Length());
}

// Tests the copy c'tor.
TEST(MyString, CopyConstructor) {
  const MyString s1(kHelloString);
  const MyString s2 = s1;
  EXPECT_EQ(0, strcmp(s2.c_string(), kHelloString));
}

// Tests the Set method.
TEST(MyString, Set) {
  MyString s;

  s.Set(kHelloString);
  EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));

  // Set should work when the input pointer is the same as the one
  // already in the MyString object.
  s.Set(s.c_string());
  EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));

  // Can we set the MyString to NULL?
  s.Set(nullptr);
  EXPECT_STREQ(nullptr, s.c_string());
}
}  // namespace
学新通

如何编译呢:
g sample2.cc sample2_unittest.cc -lgtest -std=c 14 -lgtest_main -lpthread -o test2
运行结果:
学新通
在这个sample里面对比上一个sample用到了EXPECT_STREQ(str1,str2)断言,这是用来比较str1和str2的值是否相等,从这个例子上来说,gtest对类的测试也是完全支持的。


⭐sample3

sample3-inl.h


#ifndef GOOGLETEST_SAMPLES_SAMPLE3_INL_H_
#define GOOGLETEST_SAMPLES_SAMPLE3_INL_H_

#include <stddef.h>

// Queue is a simple queue implemented as a singled-linked list.
//
// The element type must support copy constructor.
template <typename E>  // E is the element type
class Queue;

// QueueNode is a node in a Queue, which consists of an element of
// type E and a pointer to the next node.
template <typename E>  // E is the element type
class QueueNode {
  friend class Queue<E>;

 public:
  // Gets the element in this node.
  const E& element() const { return element_; }

  // Gets the next node in the queue.
  QueueNode* next() { return next_; }
  const QueueNode* next() const { return next_; }

 private:
  // Creates a node with a given element value.  The next pointer is
  // set to NULL.
  explicit QueueNode(const E& an_element)
      : element_(an_element), next_(nullptr) {}

  // We disable the default assignment operator and copy c'tor.
  const QueueNode& operator=(const QueueNode&);
  QueueNode(const QueueNode&);

  E element_;
  QueueNode* next_;
};

template <typename E>  // E is the element type.
class Queue {
 public:
  // Creates an empty queue.
  Queue() : head_(nullptr), last_(nullptr), size_(0) {}

  // D'tor.  Clears the queue.
  ~Queue() { Clear(); }

  // Clears the queue.
  void Clear() {
    if (size_ > 0) {
      // 1. Deletes every node.
      QueueNode<E>* node = head_;
      QueueNode<E>* next = node->next();
      for (;;) {
        delete node;
        node = next;
        if (node == nullptr) break;
        next = node->next();
      }

      // 2. Resets the member variables.
      head_ = last_ = nullptr;
      size_ = 0;
    }
  }
  void Enqueue(const E& element) {
    QueueNode<E>* new_node = new QueueNode<E>(element);

    if (size_ == 0) {
      head_ = last_ = new_node;
      size_ = 1;
    } else {
      last_->next_ = new_node;
      last_ = new_node;
      size_  ;
    }
  }

  // Removes the head of the queue and returns it.  Returns NULL if
  // the queue is empty.
  E* Dequeue() {
    if (size_ == 0) {
      return nullptr;
    }

    const QueueNode<E>* const old_head = head_;
    head_ = head_->next_;
    size_--;
    if (size_ == 0) {
      last_ = nullptr;
    }

    E* element = new E(old_head->element());
    delete old_head;

    return element;
  }

  // Applies a function/functor on each element of the queue, and
  // returns the result in a new queue.  The original queue is not
  // affected.
  template <typename F>
  Queue* Map(F function) const {
    Queue* new_queue = new Queue();
    for (const QueueNode<E>* node = head_; node != nullptr;
         node = node->next_) {
      new_queue->Enqueue(function(node->element()));
    }

    return new_queue;
  }

 private:
  QueueNode<E>* head_;  // The first node of the queue.
  QueueNode<E>* last_;  // The last node of the queue.
  size_t size_;         // The number of elements in the queue.

  // We disallow copying a queue.
  Queue(const Queue&);
  const Queue& operator=(const Queue&);
};

#endif  // GOOGLETEST_SAMPLES_SAMPLE3_INL_H_

学新通

sample3_unittest.cc

#include "sample3-inl.h"
#include "gtest/gtest.h"
namespace {
// 测试类继承自 testing::Test,这样这个测试类可以反复使用
class QueueTestSmpl3 : public testing::Test {
 protected:  // You should make the members protected s.t. they can be
             // accessed from sub-classes.
  // virtual void SetUp() will be called before each test is run.  You
  // should define it if you need to initialize the variables.
  // Otherwise, this can be skipped.
  void SetUp() override {
    q1_.Enqueue(1);
    q2_.Enqueue(2);
    q2_.Enqueue(3);
  }

  // virtual void TearDown() will be called after each test is run.
  // You should define it if there is cleanup work to do.  Otherwise,
  // you don't have to provide it.
  //
  // virtual void TearDown() {
  // }

  // A helper function that some test uses.
  static int Double(int n) { return 2 * n; }

  // A helper function for testing Queue::Map().
  void MapTester(const Queue<int>* q) {
    // Creates a new queue, where each element is twice as big as the
    // corresponding one in q.
    const Queue<int>* const new_q = q->Map(Double);

    // Verifies that the new queue has the same size as q.
    ASSERT_EQ(q->Size(), new_q->Size());

    // Verifies the relationship between the elements of the two queues.
    for (const QueueNode<int>*n1 = q->Head(), *n2 = new_q->Head();
         n1 != nullptr; n1 = n1->next(), n2 = n2->next()) {
      EXPECT_EQ(2 * n1->element(), n2->element());
    }

    delete new_q;
  }

  // Declares the variables your tests want to use.
  Queue<int> q0_;
  Queue<int> q1_;
  Queue<int> q2_;
};

// When you have a test fixture, you define a test using TEST_F
// instead of TEST.

// Tests the default c'tor.
TEST_F(QueueTestSmpl3, DefaultConstructor) {
  // You can access data in the test fixture here.
  EXPECT_EQ(0u, q0_.Size());
}

// Tests Dequeue().
TEST_F(QueueTestSmpl3, Dequeue) {
  int* n = q0_.Dequeue();
  EXPECT_TRUE(n == nullptr);

  n = q1_.Dequeue();
  ASSERT_TRUE(n != nullptr);
  EXPECT_EQ(1, *n);
  EXPECT_EQ(0u, q1_.Size());
  delete n;

  n = q2_.Dequeue();
  ASSERT_TRUE(n != nullptr);
  EXPECT_EQ(2, *n);
  EXPECT_EQ(1u, q2_.Size());
  delete n;
}

// Tests the Queue::Map() function.
TEST_F(QueueTestSmpl3, Map) {
  MapTester(&q0_);
  MapTester(&q1_);
  MapTester(&q2_);
}
}  // namespace

学新通

如何编译呢?👇
g sample3_unittest.cc -lgtest -std=c 14 -lgtest_main -lpthread -o test3
运行效果:
学新通

1、这个sample中用到了测试类继承自testing::Test,主要是为了下面两个原因:

  • 设置共享的状态和资源:你可能需要创建一些共享的对象,在多个测试用例中复用它们。你可以在 SetUp() 函数中创建这些对象,然后在 TearDown() 函数中释放它们。这样可以保证这些对象在所有测试用例执行之前创建,并且在所有测试用例执行之后销毁。

  • 提供公共的函数和工具函数:你可能需要在多个测试用例中使用相同的函数和工具函数。将这些函数和工具函数放在测试类中可以使其易于共享和重用。

2、继承自 testing::Test 的测试类可以定义 SetUp() 和 TearDown() 函数。在每个测试用例之前和之后,Google Test 会自动调用这些函数,以帮助你设置和清理测试环境。

3、此外,如果你需要在测试用例中使用类的成员函数或变量,那么继承自 testing::Test 可以使这些成员函数和变量在测试用例中可见。
如果将测试类的成员变量设置为protectedpublic,测试方法就可以直接访问这些成员变量了。使用public或protected可以方便地访问测试类的成员变量和方法,但是会暴露内部实现细节。而使用private则可以更好地封装内部实现,但是需要提供一些公共的接口来进行测试。在实际应用中,需要根据实际情况来选择合适的访问控制方式。

4、不知道大家注意到没有,这里用到的宏是TEST_F而不是原来的TEST,在使用测试夹具(继承自testing::Test的测试类)的时候一般是使用TEST_F
①TEST宏和TEST_F宏都是gtest库中用于定义测试用例的宏,它们的主要区别在于测试用例的初始化和清理方式不同。TEST宏用于定义一个独立的测试用例,TEST_F宏用于定义一个测试夹具(fixture),它提供了一种在测试用例之间共享状态和代码的方法。TEST_F的第一个参数就不能乱取了,必须是测试夹具(继承自testing::Test的测试类),在测试用例运行前,会对测试夹具进行初始化操作(SetUp() ),在运行时会通过第一个参数创建一个测试夹具的实例,在运行后,会对测试夹具进行清理(TearDown())。
②TEST宏用于定义独立的测试用例,而TEST_F宏用于定义一个测试夹具,可以在测试用例之间共享状态和代码(其实就是说一些初始条件不用反复设置代码复用性好,不自己单独创建实例,让你的测试代码更加易读和易维护。)。选择使用哪种宏取决于你的测试需要。如果你的测试需要在运行之前进行一些初始化操作,或者需要在测试用例之间共享状态,那么使用TEST_F宏是更合适的选择。如果你的测试用例之间没有共享状态,那么使用TEST宏即可。


gtest的相关内容先讲到这,如果以后在开发过程中遇到其他高级用法或者是文章中没提到的方法,到时候再继续更新,感谢您看到这,希望这篇文章对您能有所帮助,谢谢!

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgijffg
系列文章
更多 icon
同类精品
更多 icon
继续加载