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