前言

有时候在测试的时候,我们会在测试前做一些初始化活动,和测试后做一些清理工作,gtest提供了多种事件机制,非常方便我们在案例之前或之后做一些操作。总结一下gtest的事件一共有3种:

  1. 全局的,所有案例执行前后。
  2. TestSuite级别的,在某一批案例中第一个案例前,最后一个案例执行后。
  3. TestCase级别的,每个TestCase前后。

接下来按照倒叙3→2→1介绍如何使用事件机制

TestCase事件 

TestCase事件是挂在每个案例执行前后的,实现方式和上面的几乎一样,不过需要实现的是SetUp方法和TearDown方法:

1. SetUp()方法在每个TestCase之前执行

2. TearDown()方法在每个TestCase之后执行

演示代码(Linux环境)

main.cpp

  1. #include "sample-inl.h"
  2. #include "gtest/gtest.h"
  3. namespace {
  4. class QueueTestSmpl : public testing::Test {
  5. protected:
  6.  
  7. virtual void SetUp() {
  8. //q0_.Enqueue(1);
  9. q1_.Enqueue();
  10. q1_.Enqueue();
  11. q2_.Enqueue();
  12. }
  13.  
  14. virtual void TearDown() {
  15. }
  16.  
  17. static int Double(int n) {
  18. return * n;
  19. }
  20.  
  21. // A helper function for testing Queue::Map().
  22. void MapTester(const Queue<int> * q) {
  23. // Creates a new queue, where each element is twice as big as the
  24. // corresponding one in q.
  25. const Queue<int> * const new_q = q->Map(Double);
  26.  
  27. // Verifies that the new queue has the same size as q.
  28. ASSERT_EQ(q->Size(), new_q->Size());
  29.  
  30. // Verifies the relationship between the elements of the two queues.
  31. for (const QueueNode<int>*n1 = q->Head(), *n2 = new_q->Head();
  32. n1 != nullptr; n1 = n1->next(), n2 = n2->next()) {
  33. EXPECT_EQ( * n1->element(), n2->element());
  34. }
  35.  
  36. delete new_q;
  37. }
  38.  
  39. // Declares the variables your tests want to use.
  40. Queue<int> q0_;
  41. Queue<int> q1_;
  42. Queue<int> q2_;
  43. };
  44.  
  45. // Tests Dequeue().
  46. TEST_F(QueueTestSmpl, Dequeue) {
  47. int * n = q0_.Dequeue();
  48. EXPECT_TRUE(n == nullptr);
  49.  
  50. n = q1_.Dequeue();
  51. ASSERT_TRUE(n != nullptr);
  52. EXPECT_EQ(, *n);
  53. EXPECT_EQ(1u, q1_.Size());
  54. delete n;
  55.  
  56. n = q1_.Dequeue();
  57. ASSERT_TRUE(n != nullptr);
  58. EXPECT_EQ(, *n);
  59. EXPECT_EQ(0u, q1_.Size());
  60. delete n;
  61.  
  62. n = q2_.Dequeue();
  63. ASSERT_TRUE(n != nullptr);
  64. EXPECT_EQ(, *n);
  65. EXPECT_EQ(0u, q2_.Size());
  66. delete n;
  67. }
  68. } // namespace

sample-inl.h

  1. #ifndef GTEST_SAMPLES_SAMPLE_INL_H_
  2. #define GTEST_SAMPLES_SAMPLE_INL_H_
  3.  
  4. #include <stddef.h>
  5.  
  6. template <typename E> // E is the element type
  7. class Queue;
  8.  
  9. template <typename E> // E is the element type
  10. class QueueNode {
  11. friend class Queue<E>;
  12.  
  13. public:
  14. // Gets the element in this node.
  15. const E& element() const { return element_; }
  16.  
  17. // Gets the next node in the queue.
  18. QueueNode* next() { return next_; }
  19. const QueueNode* next() const { return next_; }
  20.  
  21. private:
  22. // Creates a node with a given element value. The next pointer is
  23. // set to NULL.
  24. explicit QueueNode(const E& an_element)
  25. : element_(an_element), next_(nullptr) {}
  26.  
  27. // We disable the default assignment operator and copy c'tor.
  28. const QueueNode& operator = (const QueueNode&);
  29. QueueNode(const QueueNode&);
  30.  
  31. E element_;
  32. QueueNode* next_;
  33. };
  34.  
  35. template <typename E> // E is the element type.
  36. class Queue {
  37. public:
  38. // Creates an empty queue.
  39. Queue() : head_(nullptr), last_(nullptr), size_() {}
  40.  
  41. // D'tor. Clears the queue.
  42. ~Queue() { Clear(); }
  43.  
  44. // Clears the queue.
  45. void Clear() {
  46. if (size_ > ) {
  47. // 1. Deletes every node.
  48. QueueNode<E>* node = head_;
  49. QueueNode<E>* next = node->next();
  50. for (; ;) {
  51. delete node;
  52. node = next;
  53. if (node == nullptr) break;
  54. next = node->next();
  55. }
  56.  
  57. // 2. Resets the member variables.
  58. head_ = last_ = nullptr;
  59. size_ = ;
  60. }
  61. }
  62.  
  63. size_t Size() const { return size_; }
  64.  
  65. QueueNode<E>* Head() { return head_; }
  66. const QueueNode<E>* Head() const { return head_; }
  67.  
  68. QueueNode<E>* Last() { return last_; }
  69. const QueueNode<E>* Last() const { return last_; }
  70.  
  71. void Enqueue(const E& element) {
  72. QueueNode<E>* new_node = new QueueNode<E>(element);
  73.  
  74. if (size_ == ) {
  75. head_ = last_ = new_node;
  76. size_ = ;
  77. }
  78. else {
  79. last_->next_ = new_node;
  80. last_ = new_node;
  81. size_++;
  82. }
  83. }
  84.  
  85. E* Dequeue() {
  86. if (size_ == ) {
  87. return nullptr;
  88. }
  89.  
  90. const QueueNode<E>* const old_head = head_;
  91. head_ = head_->next_;
  92. size_--;
  93. if (size_ == ) {
  94. last_ = nullptr;
  95. }
  96.  
  97. E* element = new E(old_head->element());
  98. delete old_head;
  99.  
  100. return element;
  101. }
  102.  
  103. template <typename F>
  104. Queue* Map(F function) const {
  105. Queue* new_queue = new Queue();
  106. for (const QueueNode<E>* node = head_; node != nullptr;
  107. node = node->next_) {
  108. new_queue->Enqueue(function(node->element()));
  109. }
  110.  
  111. return new_queue;
  112. }
  113.  
  114. private:
  115. QueueNode<E>* head_; // The first node of the queue.
  116. QueueNode<E>* last_; // The last node of the queue.
  117. size_t size_; // The number of elements in the queue.
  118.  
  119. // We disallow copying a queue.
  120. Queue(const Queue&);
  121. const Queue& operator = (const Queue&);
  122. };
  123.  
  124. #endif // GTEST_SAMPLES_SAMPLE3_INL_H_

TestSuite事件

这个相对来说比较简单,相对上面的实现就是替换成静态的SetUpTestCase,代码

  1. class FooTest : public testing::Test {
  2. protected:
  3. //准备资源
  4. static void SetUpTestCase() {
  5. shared_resource_ = new ;
  6. }
  7. //释放资源
  8. static void TearDownTestCase() {
  9. delete shared_resource_;
  10. shared_resource_ = NULL;
  11. }
  12. // 资源
  13. static T* shared_resource_;
  14. };

  

全局事件

也是很简单的,就是继承于testing::Environment而已。

  1. class FooEnvironment: public testing::Environment
  2. {
  3. public:
  4. virtual void SetUp()
  5. {
  6. printf("Environment SetUp!\n");
  7. a = 100;
  8. }
  9. virtual void TearDown()
  10. {
  11. printf("Environment TearDown!\n");
  12. }
  13. int a; //共享数据
  14. };
  15. FooEnvironment* foo_env; //对象指针声明
  16. TEST(firstTest, first) //访问共享数据并改变它的值
  17. {
  18. printf("in the firstTest, foo_env->p is %d\n", foo_env->a);
  19. foo_env->a ++;
  20. }
  21. TEST(secondTest, second) //访问共享数据
  22. {
  23. printf("in the secondTest, foo_env->p is %d\n", foo_env->a);
  24. }
  25. int main(int argc, char* argv[])
  26. {
  27. foo_env = new FooEnvironment;
  28. testing::AddGlobalTestEnvironment(foo_env); //注册
  29. testing::InitGoogleTest(&argc, argv);
  30. return RUN_ALL_TESTS();
  31. }

  

Gtest:事件的更多相关文章

  1. JNI详解---从不懂到理解

    转载:https://blog.csdn.net/hui12581/article/details/44832651 Chap1:JNI完全手册... 3 Chap2:JNI-百度百科... 11 C ...

  2. gtest 三种事件机制

    前言: 1.首先说明gtest中事件的结构层次: 测试程序:一个测试程序只有一个main函数,也可以说是一个可执行程序是一个测试程序.该级别的事件机制会在程序的开始和结束执行. 测试套件:代表一个测试 ...

  3. gTest详解

    1. 安装使用 1.1 安装 在https://code.google.com/p/googletest/ 下载源码 进入msvc, 注意编译方式, 如果是dll, 选择 gtest-md 编译生成l ...

  4. 白盒测试之gtest第一个demo

    认识gtest工具后,关于它的使用,下面将用一个demo程序演示一下gtest的用法以及成果展示. 一.需要测试的C++代码: #include "myfunction.h" // ...

  5. gtest框架使用

    gtest文档说明: 由于公司单元测试的需要,自己花了大半天时间下载了一个gtest框架,使用了一些测试例子,总览了coderzh的玩转gtest测试框架,又看了几篇gtest博客,写下了以下内容,作 ...

  6. gtest的安装和测试[good]

    一.前言 本篇将介绍一些gtest的基本使用,包括下载,安装,编译,建立我们第一个测试Demo工程,以及编写一个最简单的测试案例. 二.下载 如果不记得网址, 直接在google里搜gtest,第一个 ...

  7. gtest简短,简单易用

    gtest它是一种跨平台的(Liunx.Mac OS X.Windows.Cygwin.Windows CE and Symbian)的C++测试框架.有google该公司宣布. gtest台上为编写 ...

  8. gtest框架

    解析gtest框架运行机制   1.前言 Google test是一款开源的白盒单元测试框架,据说目前在Google内部已在几千个项目中应用了基于该框架的白盒测试. 最近的工作是在搞一个基于gtest ...

  9. 简单易懂的单元测试框架-gtest(二)

    简介     事件机制用于在案例运行前后添加一些操作(相当于挂钩函数).目前,gtest提供了三种等级的事件,分别: 全局级,所有案例执行的前后 TestSuite级,某一个案例集的前后 TestCa ...

随机推荐

  1. k8s记录-node组件部署(十)

    1)CA 证书配置登录 192.168.0.1 app 用户下cd ssl/kubernetes#注意修改 KUBE_HOME,BOOTSTRAP_TOKEN #与 3.5 3)token 一致,KU ...

  2. jzy3D从入门到弃坑_3使用jzy3D0.9画2D散点图--多条线条

    jzy3D从入门到弃坑_3 使用jzy3D0.9画2D散点图--多条线条 觉得有用的话,欢迎一起讨论相互学习~Follow Me 主要思路 将多个线条的二维数组读入Scatter对象 存入到同一个画布 ...

  3. JavaScript 有用的代码片段和 trick

    浮点数取整 const x = 123.4545; x >> 0; // 123 ~~x; // 123 x | 0; // 123 Math.floor(x); // 123 注意:前三 ...

  4. Oracle中恢复drop掉的表中的数据

    今天同事不小心把生产上的一张表直接drop掉了,没有做备份,哥们慌的一匹,来找我这个小白来帮忙解决,于是心血来潮简单总结一下. 其实在oralce中,用drop删掉一张表,其实不会真正的删除,只是把表 ...

  5. 001 SringBoot基础知识及SpringBoot整合Mybatis

    1.原有Spring优缺点分析 (1)优点 Spring是Java企业版(Java Enterprise Edition,JEE,也称J2EE)的轻量级代替品.无需开发重量级的Enterprise J ...

  6. [转帖]Java中重写和重载与多态的关系

    Java中重写和重载与多态的关系 2019-09-05 00:57:41 留下一天今天 阅读数 67  收藏 更多 分类专栏: java进阶之路   版权声明:本文为博主原创文章,遵循CC 4.0 B ...

  7. [转帖]Beyond compare4密钥

    Beyond compare4密钥 https://blog.csdn.net/lemontree1945/article/details/92963423 学习一下 最近想破解水卡.... w4G- ...

  8. Docker之Alpine制作镜像且上传至阿里云

    目的: Alpine制作jdk镜像 Alpine制作jre镜像(瘦身) Docker镜像上传至阿里云 Alpine制作jdk镜像 alpine Linux简介 Alpine Linux是一个轻型Lin ...

  9. Bitnami配置域名访问

    安装完成Bitnami后,需要执行以下命令将默认目录改为/wordpress: E:\Bitnami\wordpress-5.2.2-0\apps\wordpress\bnconfig.exe --a ...

  10. N皇后问题的python实现

    数据结构中常见的问题,最近复习到了,用python做一遍. # 检测(x,y)这个位置是否合法(不会被其他皇后攻击到) def is_attack(queue, x, y): for i in ran ...