Google open-source C ++ unit test framework: Google test series (gtest) 3-event mechanism

Source: Internet
Author: User
I. Preface

Gtest provides multiple event mechanisms to facilitate operations before or after the case. To sum up, there are three types of gtest events:

1. Global, before and after all cases.

2. testsuite level, before the first case in a batch of cases, after the last case.

3. testcase level, before and after each testcase.

Ii. Global events

To implement global events, you must write a class that inherits the testing: Environment class and implements the setup and teardown methods.

1. The setup () method is executed before all cases are executed.

2. the teardown () method is executed after all cases are executed.

Class Fooenvironment: Public Testing: Environment
{
Public :
Virtual   Void Setup ()
{
STD: cout <   " Foo fooenvironment setup "   < STD: Endl;
}
Virtual   Void Teardown ()
{
STD: cout <   " Foo fooenvironment teardown "   < STD: Endl;
}
};

 

Of course, this is not enough. We also need to tell gtest to add this global event. We need to use the testing: addglobaltestenvironment method in the main function to upload the event. That is to say, we can write many such classes and then mount all their events.

Int _ Tmain ( Int Argc, _ tchar * Argv [])
{
Testing: addglobaltestenvironment ( New Fooenvironment );
Testing: initgoogletest ( & Argc, argv );
Return Run_all_tests ();
}

 

Iii. testsuite events

We need to write a class that inherits testing: test and then implements two static methods.

 
1. The setuptestcase () method is executed before the first testcase.
2. The teardowntestcase () method is executed after the last testcase.
Class Footest: Public Testing: test {
  Protected :
Static   Void Setuptestcase (){
Shared_resource _ =   New ;
}
Static   Void Teardowntestcase (){
Delete shared_resource _;
Shared_resource _ = NULL;
}
// Some expensive resource shared by all tests.
Static T * Shared_resource _;
};
 
When writing test cases, we need to use the macro test_f. The first parameter must be the name of the class above, representing a testsuite.
Test_f (footest, test1)
{
// You can refer to shared_resource here
}
Test_f (footest, Test2)
{
// You can refer to shared_resource here
}

Iv. testcase event

Testcase events are stored before and after each case execution. The implementation method is almost the same as above, but the setup and teardown methods must be implemented:

1. The setup () method is executed before each testcase.

2. the teardown () method is executed after each testcase.

Class Foocalctest: Public Testing: Test
{
Protected :
Virtual   Void Setup ()
{
M_foo.init ();
}
Virtual   Void Teardown ()
{
M_foo.finalize ();
}

Foocalc m_foo;
};

Test_f (foocalctest, handlenonezeroinput)
{
Expect_eq (4, M_foo.calc (12,16));
}

Test_f (foocalctest, handlenonezeroinput_error)
{
Expect_eq (5, M_foo.calc (12,16));
}

 

V. Summary

The three event mechanisms provided by gtest are very simple and flexible. At the same time, by inheriting the test class and using the test_f macro, we can share some common methods and resources between cases. This makes our cases more concise and clear.

Series links:

1. Go to Google's open-source C ++ unit testing framework, one of the Google test series (gtest)-First known as gtest

2. Go to Google open-source C ++ unit test framework Google test series (gtest) 2-Assertions

3. Go to Google open-source C ++ unit test framework Google test series (gtest) 3-event mechanism

4. Go to Google's open-source C ++ unit test framework: four parameters of the Google test series (gtest)

5. go to Google open-source C ++ unit testing framework Google test series (gtest) 5-death Testing

6. go to Google open-source C ++ unit testing framework-Google test series (gtest)-running parameters

7. Go to Google open-source C ++ unit test framework Google test series (gtest) 7-in-depth analysis of gtest

8. go to Google open-source C ++ unit testing framework Google test series (gtest)-build your own unit testing framework

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.