zoukankan      html  css  js  c++  java
  • gtest 三种事件机制

    前言:

      1.首先说明gtest中事件的结构层次:

      

      测试程序:一个测试程序只有一个main函数,也可以说是一个可执行程序是一个测试程序。该级别的事件机制会在程序的开始和结束执行。

      测试套件:代表一个测试用例的集合体,该级别的事件机制会在整体的测试案例开始可结束执行。

      测试用例:该级别的事件机制会在每个测试用例开始和结束都执行。

      gtest中的事件机制是指在测试前和测试后提供给用户自行添加操作的机制,而且次机制也可用让同一测试套件下的测试用例共享数据。

      

    一、全局的事件机制(针对整个测试程序)

      实现全局的事件机制,需要创建一个自己的类,然后继承testing::Environment类,然后分别实现成员函数SetUp()和TearDown(),同时在main函数内进行调用,即"testing::AddGlobalTestEnvironment(new MyEnvironment);",通过调用函数我们可以添加多个全局的事件机制。

      SetUp()函数是在所有测试开始前执行。

      TearDown()函数是在所有测试结束后执行。

      示例:  

     1 /***********************************************
     2 
     3     Filename       : test.cpp
     4     Author         :
     5     Description    :
     6     Create Data    : 2018-10-21 00:42:34
     7     Modfiy History : 2018-10-21 00:42:34
     8 
     9 ***********************************************/
    10 
    11 #include <iostream>
    12 
    13 #include <gtest/gtest.h>
    14 
    15 using namespace std;
    16 
    17 class MyEnvironment0 : public testing::Environment
    18 {
    19     public:
    20         virtual void SetUp()
    21         {
    22             cout << "Global event0 : start" << endl;
    23         }
    24 
    25         virtual void TearDown()
    26         {
    27             cout << "Global event0 : end" << endl;
    28         }
    29 };
    30 
    31 class MyEnvironment1 : public testing::Environment
    32 {
    33     public:
    34         virtual void SetUp()
    35         {
    36             cout << "Global event1 : start" << endl;
    37         }
    38 
    39         virtual void TearDown()
    40         {
    41             cout << "Global event1 : end" << endl;
    42         }
    43 };
    44 
    45 TEST(GlobalTest0, test0)
    46 {
    47     EXPECT_EQ(1, 1);
    48 };
    49 
    50 
    51 TEST(GlobalTest0, test1)
    52 {
    53     EXPECT_EQ(2, 2);
    54 };
    55 
    56 TEST(GlobalTest1, test0)
    57 {
    58     EXPECT_EQ(3, 3);
    59 };
    60 
    61 int main(int argc, char *argv[])
    62 {
    63     testing::AddGlobalTestEnvironment(new MyEnvironment0);
    64     testing::AddGlobalTestEnvironment(new MyEnvironment1);
    65 
    66     testing::InitGoogleTest(&argc, argv);
    67 
    68     return RUN_ALL_TESTS();
    69 }

      编译命令及结果:

    二、局部的事件机制(针对一个个测试套件)

      测试套件的事件机制我们同样需要去创建一个类,继承testing::Test,实现两个静态函数SetUpTestCase()和TearDownTestCase(),测试套件的事件机制不需要像全局事件机制一样在main注册,而是需要将我们平时使用的TEST宏改为TEST_F宏。

      SetUpTestCase()函数是在测试套件第一个测试用例开始前执行。

      TearDownTestCase()函数是在测试套件最后一个测试用例结束后执行。

       需要注意TEST_F的第一个参数使我们创建的类名,也就是当前测试套件的名称。

      示例:

     1 /***********************************************
     2 
     3     Filename       : test.cpp
     4     Author         :
     5     Description    :
     6     Create Data    : 2018-10-21 01:05:17
     7     Modfiy History : 2018-10-21 01:05:17
     8 
     9 ***********************************************/
    10 
    11 #include <iostream>
    12 
    13 #include <gtest/gtest.h>
    14 
    15 using namespace std;
    16 
    17 class MyTestSuite0 : public testing::Test
    18 {
    19     protected:
    20         static void SetUpTestSuite()
    21         {
    22             cout << "TestSuite event0 : start" << endl;
    23         }
    24 
    25         static void TearDownTestSuite()
    26         {
    27             cout << "TestSuite event0 : end" << endl;
    28         }
    29 };
    30 
    31 class MyTestSuite1 : public testing::Test
    32 {
    33     protected:
    34         static void SetUpTestSuite()
    35         {
    36             cout << "TestSuite event1 : start" << endl;
    37         }
    38 
    39         static void TearDownTestSuite()
    40         {
    41             cout << "TestSuite event1 : end" << endl;
    42         }
    43 };
    44 
    45 TEST_F(MyTestSuite0, test0)
    46 {
    47     EXPECT_EQ(1, 1);
    48 }
    49 
    50 TEST_F(MyTestSuite1, test0)
    51 {
    52     EXPECT_EQ(1, 1);
    53 }
    54 
    55 TEST_F(MyTestSuite0, test1)
    56 {
    57     EXPECT_EQ(1, 1);
    58 }
    59 
    60 TEST_F(MyTestSuite1, test1)
    61 {
    62     EXPECT_EQ(1, 1);
    63 }
    64 
    65 int main(int argc, char *argv[])
    66 {
    67     testing::InitGoogleTest(&argc, argv);
    68 
    69     return RUN_ALL_TESTS();
    70 }

      编译命令及结果:

    三、个体的事件机制(针对一个个测试用例)

      测试用例的事件机制的创建和测试套件的基本一样,不同地方在于测试用例实现的两个函数分别是SetUp()和TearDown(),这两个函数不是静态函数了。

      SetUp()函数是在一个测试用例的开始前执行。

      TearDown()函数是在一个测试用例的结束后执行。

       示例:

     1 /***********************************************
     2 
     3     Filename       : test.cpp
     4     Author         :
     5     Description    :
     6     Create Data    : 2018-10-21 01:23:12
     7     Modfiy History : 2018-10-21 01:23:12
     8 
     9 ***********************************************/
    10 
    11 #include <iostream>
    12 
    13 #include <gtest/gtest.h>
    14 
    15 using namespace std;
    16 
    17 class MyTestCase0 : public testing::Test
    18 {
    19     protected:
    20         virtual void SetUp()
    21         {
    22             cout << "TestCase event0 : start" << endl;
    23         }
    24 
    25         virtual void TearDown()
    26         {
    27             cout << "TestCase event0 : end" << endl;
    28         }
    29 };
    30 
    31 class MyTestCase1 : public testing::Test
    32 {
    33     protected:
    34         virtual void SetUp()
    35         {
    36             cout << "TestCase event1 : start" << endl;
    37         }
    38         virtual void TearDown()
    39         {
    40             cout << "TestCase event1 : end" << endl;
    41         }
    42 };
    43 
    44 TEST_F(MyTestCase0, test0)
    45 {
    46     EXPECT_EQ(1, 1);
    47 }
    48 
    49 TEST_F(MyTestCase0, test1)
    50 {
    51     EXPECT_EQ(1, 1);
    52 }
    53 
    54 TEST_F(MyTestCase1, test0)
    55 {
    56     EXPECT_EQ(1, 1);
    57 }
    58 
    59 TEST_F(MyTestCase1, test1)
    60 {
    61     EXPECT_EQ(1, 1);
    62 }
    63 
    64 int main(int argc, char *argv[])
    65 {
    66     testing::InitGoogleTest(&argc, argv);
    67 
    68     return RUN_ALL_TESTS();
    69 }

      编译命令及结果:

    总结:

      gtest的三种事件机制总的来说还是简单的,而且也比较灵活,通过上面的例子也能看出我们可以在事件机制中实现一些资源共享,使我们的测试更加灵活。

  • 相关阅读:
    leetcode 13. Roman to Integer
    python 判断是否为有效域名
    leetcode 169. Majority Element
    leetcode 733. Flood Fill
    最大信息系数——检测变量之间非线性相关性
    leetcode 453. Minimum Moves to Equal Array Elements
    leetcode 492. Construct the Rectangle
    leetcode 598. Range Addition II
    leetcode 349. Intersection of Two Arrays
    leetcode 171. Excel Sheet Column Number
  • 原文地址:https://www.cnblogs.com/jiangyibo/p/9825610.html
Copyright © 2011-2022 走看看