前言

在单元测试中,我们经常需要在某个测试套件、测试用例或者整个测试运行之前进行前置条件设置及检查,或者运行之后对运行结果进行校验等操作。在gtest中,称之为事件机制。gtest将事件按照作用的范围不同进行划分,从大到小总共分为3个层次:
1)整个测试层面,即在测试工程开始前和结束后进行;
2)测试套件层面,即在某个测试套件开始前和结束后进行;
3)测试用例层面,即在某个测试用例开始前和结束后进行;
2、测试层面事件实现
要实现测试层面的事件,我们需要继承testing::Environment类,首先我们来看一下这个类的定义:   
[cpp] view plain copy
  1. class Environment {
  2. public:
  3. virtual ~Environment() {}
  4. // Override this to define how to set up the environment.
  5. virtual void SetUp() {}
  6. // Override this to define how to tear down the environment.
  7. virtual void TearDown() {}
  8. private:
  9. struct Setup_should_be_spelled_SetUp {};
  10. virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; }
  11. };
这个类中有两个虚函数:SetUp和TearDown。我们的子类只需要实现这个方法即可。其中在SetUp方法中实现所有测试启动之前需要完成的操作,而TearDown函数中实现所有测试运行结束后需要进行的操作。例如:
[cpp] view plain copy
  1. class GlobalEvent2 :
  2. public testing::Environment
  3. {
  4. public:
  5. virtual void SetUp()
  6. {
  7. cout << "Before any case, Global 2" << endl;
  8. }
  9. virtual void TearDown()
  10. {
  11. cout << "After all cases done, Global 2" << endl;
  12. }
  13. };
然后,在main函数中,在RUN_ALL_TESTS()之前,我们调用如下语句:
testing::AddGlobalTestEnvironment(new GlobalEnvent);
将这个测试层面的的事件添加到事件列表即可。这样,在测试执行之前,系统会先执行GlobalEvent2的SetUp方法;在所有测试用例执行完之后,系统会执行GlobalEvent2的TearDown方法。另外,我们可以定义任意多个继承自testing::Environment的子类,以实现不同的全局事件。所有的子类的SetUp按照我们调用testing::AddGlobalTestEnvironment添加它们的先后顺序执行,而TearDown的执行顺序则与添加顺序相反。
3、测试套件层面事件
要在测试套件层面上定义事件,我们需要继承testing::Test类,并覆盖它的静态方法:SetUpTestCase和TearDownTestCase.在继续之前我们首先看看testing::Test类的定义:
[cpp] view plain copy
  1. class GTEST_API_ Test {
  2. public:
  3. friend class TestInfo;
  4. // Defines types for pointers to functions that set up and tear down
  5. // a test case.
  6. typedef internal::SetUpTestCaseFunc SetUpTestCaseFunc;
  7. typedef internal::TearDownTestCaseFunc TearDownTestCaseFunc;
  8. // The d'tor is virtual as we intend to inherit from Test.
  9. virtual ~Test();
  10. // Sets up the stuff shared by all tests in this test case.
  11. //
  12. // Google Test will call Foo::SetUpTestCase() before running the first
  13. // test in test case Foo.  Hence a sub-class can define its own
  14. // SetUpTestCase() method to shadow the one defined in the super
  15. // class.
  16. static void SetUpTestCase() {}
  17. // Tears down the stuff shared by all tests in this test case.
  18. //
  19. // Google Test will call Foo::TearDownTestCase() after running the last
  20. // test in test case Foo.  Hence a sub-class can define its own
  21. // TearDownTestCase() method to shadow the one defined in the super
  22. // class.
  23. static void TearDownTestCase() {}
  24. // Returns true iff the current test has a fatal failure.
  25. static bool HasFatalFailure();
  26. // Returns true iff the current test has a non-fatal failure.
  27. static bool HasNonfatalFailure();
  28. // Returns true iff the current test has a (either fatal or
  29. // non-fatal) failure.
  30. static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); }
  31. // Logs a property for the current test, test case, or for the entire
  32. // invocation of the test program when used outside of the context of a
  33. // test case.  Only the last value for a given key is remembered.  These
  34. // are public static so they can be called from utility functions that are
  35. // not members of the test fixture.  Calls to RecordProperty made during
  36. // lifespan of the test (from the moment its constructor starts to the
  37. // moment its destructor finishes) will be output in XML as attributes of
  38. // the <testcase> element.  Properties recorded from fixture's
  39. // SetUpTestCase or TearDownTestCase are logged as attributes of the
  40. // corresponding <testsuite> element.  Calls to RecordProperty made in the
  41. // global context (before or after invocation of RUN_ALL_TESTS and from
  42. // SetUp/TearDown method of Environment objects registered with Google
  43. // Test) will be output as attributes of the <testsuites> element.
  44. static void RecordProperty(const std::string& key, const std::string& value);
  45. static void RecordProperty(const std::string& key, int value);
  46. protected:
  47. // Creates a Test object.
  48. Test();
  49. // Sets up the test fixture.
  50. virtual void SetUp();
  51. // Tears down the test fixture.
  52. virtual void TearDown();
  53. private:
  54. // Returns true iff the current test has the same fixture class as
  55. // the first test in the current test case.
  56. static bool HasSameFixtureClass();
  57. // Runs the test after the test fixture has been set up.
  58. //
  59. // A sub-class must implement this to define the test logic.
  60. //
  61. // DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM.
  62. // Instead, use the TEST or TEST_F macro.
  63. virtual void TestBody() = 0;
  64. // Sets up, executes, and tears down the test.
  65. void Run();
  66. // Deletes self.  We deliberately pick an unusual name for this
  67. // internal method to avoid clashing with names used in user TESTs.
  68. void DeleteSelf_() { delete this; }
  69. // Uses a GTestFlagSaver to save and restore all Google Test flags.
  70. const internal::GTestFlagSaver* const gtest_flag_saver_;
  71. // Often a user mis-spells SetUp() as Setup() and spends a long time
  72. // wondering why it is never called by Google Test.  The declaration of
  73. // the following method is solely for catching such an error at
  74. // compile time:
  75. //
  76. //   - The return type is deliberately chosen to be not void, so it
  77. //   will be a conflict if a user declares void Setup() in his test
  78. //   fixture.
  79. //
  80. //   - This method is private, so it will be another compiler error
  81. //   if a user calls it from his test fixture.
  82. //
  83. // DO NOT OVERRIDE THIS FUNCTION.
  84. //
  85. // If you see an error about overriding the following function or
  86. // about it being private, you have mis-spelled SetUp() as Setup().
  87. struct Setup_should_be_spelled_SetUp {};
  88. virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; }
  89. // We disallow copying Tests.
  90. GTEST_DISALLOW_COPY_AND_ASSIGN_(Test);
  91. };
从testing::Test类的声明可以看到,SetUpTestCase和TearDownTestCase为静态方法,并且它们的注释也很详细。在测试套件的第一个测试用例开始前,SetUpTestCase函数会被调用,而在测试套件中的最后一个测试用例运行结束后,TearDownTestCase函数会被调用。
4、测试用例层面的事件
要实现单个测试用例的事件,我们需要同样需要继承testing::Test类,并实现它的protected virtual方法SetUp和TearDown。gtest在运行这个测试用例之前,会首先调用SetUp方法,然后在测试用例结束之后,调用TearDown方法。
5、总结
通过gtest事件机制,我们可以让gtest在运行测试、测试套件、测试用例的前后分别运行指定的代码段。这一点很有用,比如在单元测试中,我们可以将初始化操作放入SetUp函数中,而资源回收等操作方在TearDown函数中实现,这样可以使得我们在测试用例中只需专注于测试即可。

gtest 中Setup TearDown SetUpTestCase和TearDownTestCase 的区别相关推荐

  1. 三、pytest接口自动化之pytest中setup/teardown,setup_class/teardown_class讲解

    pytest框架实现的前后置的处理(固件,夹具),很多种方式,常见的三种. 一.setup/teardown,setup_class/teardown_class 为什么需要这些功能? Class T ...

  2. python中setup函数的用法_python学习之setUp函数和tearDown函数

    1,setUp():就是在一个类中最先被调用的函数,每次执行一个函数都要先执行这个函数,有几个函数就被调用几次,与放的位置无关,随便放到哪里都会先执行这个函数 2,tearDown():就是在一个类中 ...

  3. 第六:Pytest中的setup/teardown

    简介 学过unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例开始前和结束后都去执行一次. 当然还有更高级一点的setupClass和teardownClass,需 ...

  4. 5.pytest中setup和teardown

    文章目录 前言 函数级别 模块级别 类级别 前言 用过unittest的童鞋都知道,有两个前置方法,两个后置方法:分别是 setup() setupClass() teardown() teardow ...

  5. Pytest setup teardown

    目录 如何实现xunit样式setup\teardown 模块级setup/teardown 类级别setup/teardown 方法和功能级别setup/teardown 如何实现xunit样式se ...

  6. Pytest测试框架(二):pytest 的setup/teardown方法

    系列文章目录 Pytest测试框架(一):pytest安装及用例执行 Pytest测试框架(二):pytest 的setup/teardown方法 Pytest测试框架(三):pytest fixtu ...

  7. setup/teardown 的用法

    # 方法级别函数 setup_method/teardown_method和setup/teardown对类有效,也位于类中,这两个效果一样,在测试类中每个测试方法前后调用一次.# 每条用例都要执行一 ...

  8. Cacti 插件中setup.php 文件的编写

    Cacti 插件中setup.php 文件的编写 名词: 初始化函数   预定义函数  cacti 插件存放在 /plugins 目录,由setup.php与cacti 做关联调用; setup.ph ...

  9. vue3.0中setup使用(两种用法)

    这篇文章主要介绍了vue3.0中setup使用,本文通过两种用法给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下 一.setup函数的特性以及作用 可以确定的是 V ...

最新文章

  1. 如何处理海量数据(上):从并发编程到分布式系统
  2. Python脚本导出为exe程序
  3. hdu 3613 扩展kmp+回文串
  4. Java 读取指定目录下的文件名和目录名
  5. MFC中SQLite数据库的使用
  6. splice slice
  7. Java进阶3. 内存回收机制
  8. 【常见错误】--Nltk使用错误
  9. react-native-router-flux 使用详解(三)
  10. morhpia(4)-更新
  11. 51Nod1344走格子
  12. 【设计模式】迭代器模式
  13. 百元百鸡 //构造结构体变量
  14. 苹果macbookpro多少钱_买一套可用的苹果全家桶,要花多少钱?
  15. ad网络标号怎么用_altium designer网络标号的作用范围
  16. php halt,thinkphp-调试halt
  17. es 多索引联合查询_ES 的跨索引查询详细讲解
  18. 华为云备份显示服务器异常(1008),云盘服务器异常
  19. 学习大数据开发零基础是不是限制,小白能否快速学会?附上学习路线图
  20. uc浏览器黑莓java下载安装_(黑莓软件)黑莓最新版UC浏览器下载安装!8.1最新版UC浏览器!...

热门文章

  1. 华为首次自曝“天才少年”成果:入职不到一年算法研究用于千万台 Mate/P 系列手机,网友:值 200 万年薪
  2. 算法基础系列第三章——图论之最小生成树问题
  3. 微信小程序校园生活小助手+后台管理系统前后分离VUE
  4. js隐藏显示div页面方法
  5. Vue+element实现图片的上传与显示
  6. JS 随机生成字符串 数字+字母组合
  7. c语言计算利息答案是0.0,ACCP北大青鸟4.0 程序逻辑和C语言实现课本后的习题和上机题目,怎么做?...
  8. Allegro基本规则设置指导书之Electrical Total Etch Length
  9. App Indexing
  10. iOS开发——仿微信图片浏览交互的实现(向下拖拽图片退出图片浏览器)