Sample Code for gtest parameterization Test

Source: Internet
Author: User

in the Google open-source C ++ unit testing framework, Google test series (gtest) 4-parameterization has introduced how to use gtest for parametric testing. @ Xlinker is required on Twitter. Here is a complete parameterized example. This example was also written when I first learned about gtest, and also appeared in the series "play fun. Finally, I will attach the entire Demo project, which contains some other examples. At the beginning, you can try my demo project directly. If you have any questions, please submit them. The following is an example of parameterized test using the test_p macro:

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> # include " stdafx. h "
# include " Foo. h "
# include gtest / gtest. h >

ClassIsprimeparamtest:Public: Testing: testwithparam<Int>
{

};

// parameter tests are not used, write five times like this
test (isprimetest, handletruereturn)
{< br> expect_true (isprime ( 3 ));
expect_true (isprime ( 5 );
expect_true (isprime ( 11 ));
expect_true (isprime ( 23 );
expect_true (isprime ( 17 );
}

// to use parameterization testing, you only need:
test_p (isprimeparamtest, handletruereturn)
{< br> int n = getparam ();
expect_true (isprime (n);
}

//Define parameters
Instantiate_test_case_p (truereturn, isprimeparamtest, testing: values (3,5,11,23,17));

// -----------------------
// More complex parameter structure

Struct Numberpair
{
Numberpair ( Int _, Int _ B)
{
A = _;
B = _ B;
}
Int A;
Int B;
};

ClassFooparamtest:Public: Testing: testwithparam<Numberpair>
{

};

Test_p (fooparamtest, handlethreereturn)
{
Foocalc Foo;
Numberpair pair=Getparam ();
Expect_eq (3, Foo. Calc (pair. A, pair. B ));
}

Instantiate_test_case_p (threereturn, fooparamtest, testing: values (numberpair (12,15), Numberpair (18,21)));

Complete example project:

/Files/coderzh/code/gtest_demo.rar

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.