Gtest framework usage

Source: Internet
Author: User
Document directory
  • 1. numeric comparison:
  • 2. bool Value Comparison
  • 4. precisely locate the problem in case of failure

Gtest document description:

Due to the needs of the company's unit testing, I spent most of my time downloading a gtest framework. I used some test examples and gave a general view of coderzh's game-to-gtest framework, I read several more gtest blogs and wrote the following content as a memo. After all, the simple things I need are too complicated for me to review. Thanks to coderzh, his article on the google open-source framework gtest series is really good and highly recommended. Link: Workshop.

Project Overview and basic configuration:

1. modified the gtest directory structure to match the common project directory interface.
2. To use the gtest library, the header file must be located in the include \ gtest directory, and the lib file must be located in lib \ gtestd. lib.
3. Note that the project configuration must be configured in multi-thread debugging mode, that is, MTd mode. Vs20080-> properties-> Configuration properties-> code generation-> Runtime Library-> multi-thread debugging (/MTd)
4. The project must contain the header file include \ gtest. h and the library directory lib \ gtestd. lib.

//-----------------------------------------------------------------------------
Comparison Test: // basic code framework
TEST (testsuit1_test, TestCase1)
{
EXPECT_EQ (1, min (1, 2); // you can use
}
1. numeric comparison:

EXPECT_EQ (1, min (1, 2); // you can use EXPECT_EQ (1, min (10, 2); // you cannot use

The left side of the two parameters passed by EXPECT_EQ is the expected value, and the right side is your own function, that is, the function to be tested.
There are also a series of functions as follows:
// * {ASSERT | expect ct} _ EQ (expected, actual): Tests that expected = actual
// * {ASSERT | keep CT} _ NE (v1, v2): Tests that v1! = V2
// * {ASSERT | reset CT} _ LT (v1, v2): Tests that v1 <v2
// * {ASSERT | reset CT} _ LE (v1, v2): Tests that v1 <= v2
// * {ASSERT | reset CT} _ GT (v1, v2): Tests that v1> v2
// * {ASSERT | reset CT} _ GE (v1, v2): Tests that v1> = v2
The difference between assert and secondary CT is that if the secondary CT fails, it will continue to run in the current function. After the assert fails, it will exit the current function, but will not exit the entire program.
2. bool Value Comparison

// True false comparison

EXPECT_TRUE (true); // you can use EXPECT_FALSE (true); // you cannot use

If this is true, the input parameters can be printed in the false test.

EXPECT_PRED2(MutuallyPrime, m, n);//yes

3. String comparison
// String case-insensitive comparison

EXPECT_STRCASEEQ("how", "how");//yesEXPECT_STRCASEEQ("how", "HOW");//yesEXPECT_STRCASEEQ("how", "how are");//no

// String case-sensitive comparison

EXPECT_STREQ("how", "how");//yesEXPECT_STREQ("how", "HOW");//noEXPECT_STREQ("how", "how are");//no
4. precisely locate the problem in case of failure
int n = -1;  bool actualResult = Foo::Dosometing(n);  ASSERT_TRUE(actualResult) << "Call Foo::Dosometing(n) when n = " << n;


//-----------------------------------------------------------------------------
Event mechanism:

Gtest provides multiple event mechanisms to prepare test data and release related resources before the case is executed. There are three types of gtest events:
Global, before and after all cases (inheriting the testing: Environment class, implementing the SetUp and TearDown methods)
TestSuite-level, before the first case in a batch of cases, after the execution of the last case (inheriting the testing: Test class, implement the SetUpTestCase and TearDownTestCase static methods
At the TestCase level, before and after each TestCase (inheriting the testing: Test class, implementing the SetUp and TearDown methods in it)
Global:
// Global event

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;}};

Add the following in the main function:

testing::AddGlobalTestEnvironment(new FooEnvironment());

Class FooTest: public testing: Test {protected: static void SetUpTestCase () {cout <"SetUpTestCase" <endl;} static void TearDownTestCase () {cout <"TearDownTestCase" <endl;} static int m_n1_allcaseint; // This variable is public protected: virtual void SetUp () for all testsuit Cases () {cout <"SetUp" <endl;} virtual void TearDown () {cout <"TearDown" <endl;} int m_nCaseInt; // This variable is owned by each case}; int FooTest: m_n1_allcaseint = 100;

// Carefully analyze the static member variables and common member variables of c ++. m_n1_allcaseint is the member variable of the class and public; m_nCaseInt is the member variable of the object, and each object has its own
// TEST_F actually creates a class that inherits from FooTest and creates such objects. Therefore, the following two TEST_F public a static member variable, each private m_nCaseInt

TEST_F(FooTest, Test0){ cout << "Test0" << endl;}TEST_F(FooTest, Test1){ cout << "Test1" << endl;}

//-----------------------------------------------------------------------------
// Batch Parameter Test

Class ParamTest: public: testing: TestWithParam <int >{}; TEST_P (ParamTest, aLotTest) {int n = GetParam (); int m = GetParam (); cout <"m =" <m <"n =" <n <endl; EXPECT_TRUE (m = n);} INSTANTIATE_TEST_CASE_P (ParamTest, ParamTest, testing:: Values (1, 10,100,100 0, 10000); // parameter Generator

// Batch test:

INSTANTIATE_TEST_CASE_P(PreName, myTest, testing::Values(1, 10, 100, 1000, 10000));

The first parameter is the prefix of a series of test cases that are printed at the end of the test.
The second parameter is the name of the test case. It must be the same as the name of the previously defined parameterized class. In this example, It is myTest.
The third parameter is the parameter generator. In addition to the test: Values used in the preceding example, gtest also provides a series of functions generated by parameters:
Range (begin, end [, step]) ranges from begin ~ Between end, step size is step, excluding end
Values (v1, v2,..., vN) v1, v2 to vN Value
ValuesIn (container) and ValuesIn (begin, end) values from a C-type array, STL container, or iterator
Boolean () values: false and true
Combine (g1, g2 ,..., gN) This is tough, it will be g1, g2 ,... gN, g1, g2 ,... gN itself is a series of parameter generators, each from g1, g2 ,.. in gN, a value is extracted and combined into a Tuple as a parameter.

//-----------------------------------------------------------------------------
// Simple white box test example

Class Foo // to be tested class {public: Foo (void): sum (0) {} void add (int num) {sum + = num;} void print () {std:: cout <sum <std: endl;} friend class unittest_Foo; // acoustic friends class private: int sum; // private variable}; class unittest_Foo: public testing :: test {public: static void test_foo () {// you can access the private variables of A and the private function Foo a; ASSERT_EQ (. sum, 0);. add (0); ASSERT_EQ (. sum, 0);. add (1, 100); ASSERT_EQ (. sum, 100) ;}}; TEST_F (unittest_Foo, test_foo) {unittest_Foo: test_foo ();}

//-----------------------------------------------------------------------------
// Running parameters:

Gtest provides multiple methods to set running parameters. Command line parameters are commonly used.

-- Gtest_filter: filter the test cases to be executed. Wildcards are supported.
? Single Character
* Any character
-Exclude, for example,-a indicates that apart from
: Take or. For example, a: B indicates a or B.
For example:
./Foo_test: no filtering conditions are specified and all cases are run.
./Foo_test -- gtest_filter = * use wildcards to run all cases.
./Foo_test -- gtest_filter = Foo. * run all the cases in the Foo case set.
./Foo_test -- gtest_filter = Foo. test Run case Foo. test

-- Gtest_repeat: executed several times
./Foo_test -- gtest_repeat = 5 repeated executions 5 times

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.