gtest three event mechanisms

Foreword:

  1. First, explain the structural hierarchy of events in gtest:

  Test program: A test program has only one main function, and it can also be said that an executable program is a test program. Event mechanisms at this level are executed at the beginning and end of the program.

  Test suite: represents a collection of test cases, and the event mechanism at this level will start and end execution of the overall test case.

  Test case: The event mechanism at this level will be executed at the beginning and end of each test case.

  The event mechanism in gtest refers to the mechanism provided for users to add operations before and after the test, and this mechanism can also be used to share data among test cases under the same test suite.

  

1. Global event mechanism (for the entire test program)

  To implement a global event mechanism, you need to create your own class, then inherit the testing::Environment class, and then implement the member functions SetUp() and TearDown() respectively, and call them in the main function at the same time, that is, "testing::AddGlobalTestEnvironment(new MyEnvironment);", we can add multiple global event mechanisms by calling functions.

  The SetUp() function is executed before all tests start.

  The TearDown() function is executed after all tests are finished.

  Example:  

/***********************************************

    Filename       : test.cpp
    Author         :
    Description    :
    Create Data    : 2018-10-21 00:42:34
    Modfiy History : 2018-10-21 00:42:34

***********************************************/

#include <iostream>

#include <gtest/gtest.h>

using namespace std;

class MyEnvironment0 : public testing::Environment
{
    public:
        virtual void SetUp()
        {
            cout << "Global event0 : start" << endl;
        }

        virtual void TearDown()
        {
            cout << "Global event0 : end" << endl;
        }
};

class MyEnvironment1 : public testing::Environment
{
    public:
        virtual void SetUp()
        {
            cout << "Global event1 : start" << endl;
        }

        virtual void TearDown()
        {
            cout << "Global event1 : end" << endl;
        }
};

TEST(GlobalTest0, test0)
{
    EXPECT_EQ(1, 1);
};


TEST(GlobalTest0, test1)
{
    EXPECT_EQ(2, 2);
};

TEST(GlobalTest1, test0)
{
    EXPECT_EQ(3, 3);
};

int main(int argc, char *argv[])
{
    testing::AddGlobalTestEnvironment(new MyEnvironment0);
    testing::AddGlobalTestEnvironment(new MyEnvironment1);

    testing::InitGoogleTest(&argc, argv);

    return RUN_ALL_TESTS();
}

  Compile command and result:

2. Partial event mechanism (for each test suite)

  For the event mechanism of the test suite, we also need to create a class, inherit testing::Test, and implement two static functions SetUpTestCase() and TearDownTestCase(). The event mechanism of the test suite does not need to be registered in main like the global event mechanism, but We need to change the TEST macro we usually use to TEST_F macro.

  The SetUpTestCase() function is executed before the first test case of the test suite starts.

  The TearDownTestCase() function is executed after the last test case in the test suite ends.

  Note that the first parameter of TEST_F is the class name we created, which is the name of the current test suite.

  Example:

/***********************************************

    Filename       : test.cpp
    Author         :
    Description    :
    Create Data    : 2018-10-21 01:05:17
    Modfiy History : 2018-10-21 01:05:17

***********************************************/

#include <iostream>

#include <gtest/gtest.h>

using namespace std;

class MyTestSuite0 : public testing::Test
{
    protected:
        static void SetUpTestSuite()
        {
            cout << "TestSuite event0 : start" << endl;
        }

        static void TearDownTestSuite()
        {
            cout << "TestSuite event0 : end" << endl;
        }
};

class MyTestSuite1 : public testing::Test
{
    protected:
        static void SetUpTestSuite()
        {
            cout << "TestSuite event1 : start" << endl;
        }

        static void TearDownTestSuite()
        {
            cout << "TestSuite event1 : end" << endl;
        }
};

TEST_F(MyTestSuite0, test0)
{
    EXPECT_EQ(1, 1);
}

TEST_F(MyTestSuite1, test0)
{
    EXPECT_EQ(1, 1);
}

TEST_F(MyTestSuite0, test1)
{
    EXPECT_EQ(1, 1);
}

TEST_F(MyTestSuite1, test1)
{
    EXPECT_EQ(1, 1);
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);

    return RUN_ALL_TESTS();
}

  Compile command and result:

3. Individual event mechanism (for each test case)

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

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

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

  示例:

/***********************************************

    Filename       : test.cpp
    Author         :
    Description    :
    Create Data    : 2018-10-21 01:23:12
    Modfiy History : 2018-10-21 01:23:12

***********************************************/

#include <iostream>

#include <gtest/gtest.h>

using namespace std;

class MyTestCase0 : public testing::Test
{
    protected:
        virtual void SetUp()
        {
            cout << "TestCase event0 : start" << endl;
        }

        virtual void TearDown()
        {
            cout << "TestCase event0 : end" << endl;
        }
};

class MyTestCase1 : public testing::Test
{
    protected:
        virtual void SetUp()
        {
            cout << "TestCase event1 : start" << endl;
        }
        virtual void TearDown()
        {
            cout << "TestCase event1 : end" << endl;
        }
};

TEST_F(MyTestCase0, test0)
{
    EXPECT_EQ(1, 1);
}

TEST_F(MyTestCase0, test1)
{
    EXPECT_EQ(1, 1);
}

TEST_F(MyTestCase1, test0)
{
    EXPECT_EQ(1, 1);
}

TEST_F(MyTestCase1, test1)
{
    EXPECT_EQ(1, 1);
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);

    return RUN_ALL_TESTS();
}

  编译命令及结果:

总结:

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

Guess you like

Origin blog.csdn.net/sinat_33101665/article/details/128935847