值参数不限定类型,也可以是类的引用,这就可以实现对类接口的测试,一个基类可以有多个继承类,那么可以测试不同的子类功能,但是只需要写一个测试用例,然后使用参数列表实现对每个子类的测试。

使用值参数测试法去测试多个实现了相同接口(类)的共同属性(又叫做接口测试)

  1. using ::testing::TestWithParam;
  2. using ::testing::Values;
  3. typedef PrimeTable* CreatePrimeTableFunc();
  4. PrimeTable* CreateOnTheFlyPrimeTable() {
  5. return new OnTheFlyPrimeTable();
  6. }
  7. template <size_t max_precalculated>
  8. PrimeTable* CreatePreCalculatedPrimeTable() {
  9. return new PreCalculatedPrimeTable(max_precalculated);
  10. }
  11. // Inside the test body, fixture constructor, SetUp(), and TearDown() you
  12. // can refer to the test parameter by GetParam(). In this case, the test
  13. // parameter is a factory function which we call in fixture's SetUp() to
  14. // create and store an instance of PrimeTable.
  15. class PrimeTableTestSmpl7 : public TestWithParam<CreatePrimeTableFunc*> {
  16. public:
  17. ~PrimeTableTestSmpl7() override { delete table_; }
  18. void SetUp() override { table_ = (*GetParam())(); }
  19. void TearDown() override {
  20. delete table_;
  21. table_ = nullptr;
  22. }
  23. protected:
  24. PrimeTable* table_;
  25. };
  26. TEST_P(PrimeTableTestSmpl7, ReturnsFalseForNonPrimes) {
  27. EXPECT_FALSE(table_->IsPrime(-5));
  28. EXPECT_FALSE(table_->IsPrime(0));
  29. EXPECT_FALSE(table_->IsPrime(1));
  30. EXPECT_FALSE(table_->IsPrime(4));
  31. EXPECT_FALSE(table_->IsPrime(6));
  32. EXPECT_FALSE(table_->IsPrime(100));
  33. }
  34. TEST_P(PrimeTableTestSmpl7, ReturnsTrueForPrimes) {
  35. EXPECT_TRUE(table_->IsPrime(2));
  36. EXPECT_TRUE(table_->IsPrime(3));
  37. EXPECT_TRUE(table_->IsPrime(5));
  38. EXPECT_TRUE(table_->IsPrime(7));
  39. EXPECT_TRUE(table_->IsPrime(11));
  40. EXPECT_TRUE(table_->IsPrime(131));
  41. }
  42. TEST_P(PrimeTableTestSmpl7, CanGetNextPrime) {
  43. EXPECT_EQ(2, table_->GetNextPrime(1));
  44. EXPECT_EQ(3, table_->GetNextPrime(2));
  45. EXPECT_EQ(5, table_->GetNextPrime(3));
  46. EXPECT_EQ(7, table_->GetNextPrime(5));
  47. EXPECT_EQ(11, table_->GetNextPrime(7));
  48. EXPECT_EQ(131, table_->GetNextPrime(128));
  49. }
  50. // In order to run value-parameterized tests, you need to instantiate them,
  51. // or bind them to a list of values which will be used as test parameters.
  52. // You can instantiate them in a different translation module, or even
  53. // instantiate them several times.
  54. //
  55. // Here, we instantiate our tests with a list of two PrimeTable object
  56. // factory functions:
  57. #define INSTANTIATE_TEST_SUITE_P INSTANTIATE_TEST_CASE_P
  58. INSTANTIATE_TEST_SUITE_P(OnTheFlyAndPreCalculated, PrimeTableTestSmpl7,
  59. Values(&CreateOnTheFlyPrimeTable,
  60. &CreatePreCalculatedPrimeTable<1000>));

1.8sample8–值参数测试

有些时候,我们需要对代码实现的功能使用不同的参数进行测试,比如使用大量随机值来检验算法实现的正确性,或者比较同一个接口的不同实现之间的差别。gtest把“集中输入测试参数”的需求抽象出来提供支持,称为值参数化测试(Value Parameterized Test)。

参数值序列生成函数 含义
Bool() 生成序列 {false, true}
Range(begin, end[, step]) 生成序列 {begin, begin+step, begin+2*step, …} (不含 end), step默认为1
Values(v1, v2, …, vN) 生成序列 {v1, v2, …, vN}
ValuesIn(container), ValuesIn(iter1, iter2) 枚举STL container,或枚举迭代器范围 [iter1, iter2)
Combine(g1, g2, …, gN) 生成 g1, g2, …, gN的笛卡尔积,其中g1, g2, …, gN均为参数值序列生成函数(要求C++0x的<tr1/tuple>

代码实现

  1. class HybridPrimeTable : public PrimeTable {
  2. public:
  3. HybridPrimeTable(bool force_on_the_fly, int max_precalculated)
  4. : on_the_fly_impl_(new OnTheFlyPrimeTable),
  5. precalc_impl_(force_on_the_fly
  6. ? nullptr
  7. : new PreCalculatedPrimeTable(max_precalculated)),
  8. max_precalculated_(max_precalculated) {}
  9. ~HybridPrimeTable() override {
  10. delete on_the_fly_impl_;
  11. delete precalc_impl_;
  12. }
  13. bool IsPrime(int n) const override {
  14. if (precalc_impl_ != nullptr && n < max_precalculated_)
  15. return precalc_impl_->IsPrime(n);
  16. else
  17. return on_the_fly_impl_->IsPrime(n);
  18. }
  19. int GetNextPrime(int p) const override {
  20. int next_prime = -1;
  21. if (precalc_impl_ != nullptr && p < max_precalculated_)
  22. next_prime = precalc_impl_->GetNextPrime(p);
  23. return next_prime != -1 ? next_prime : on_the_fly_impl_->GetNextPrime(p);
  24. }
  25. private:
  26. OnTheFlyPrimeTable* on_the_fly_impl_;
  27. PreCalculatedPrimeTable* precalc_impl_;
  28. int max_precalculated_;
  29. };
  30. using ::testing::TestWithParam;
  31. using ::testing::Bool;
  32. using ::testing::Values;
  33. using ::testing::Combine;
  34. // To test all code paths for HybridPrimeTable we must test it with numbers
  35. // both within and outside PreCalculatedPrimeTable's capacity and also with
  36. // PreCalculatedPrimeTable disabled. We do this by defining fixture which will
  37. // accept different combinations of parameters for instantiating a
  38. // HybridPrimeTable instance.
  39. class PrimeTableTest : public TestWithParam< ::std::tuple<bool, int> > {
  40. protected:
  41. void SetUp() override {
  42. bool force_on_the_fly;
  43. int max_precalculated;
  44. std::tie(force_on_the_fly, max_precalculated) = GetParam();
  45. table_ = new HybridPrimeTable(force_on_the_fly, max_precalculated);
  46. }
  47. void TearDown() override {
  48. delete table_;
  49. table_ = nullptr;
  50. }
  51. HybridPrimeTable* table_;
  52. };

PrimeTableTest类继承于TestWithParam,是测试固件类。接收参数tuple<bool,int>,如果bool为true时,使用OnTheFlyPrimeTable类的接口,当bool变量为false时,使用PreCalculatedPrimeTable接口测试。

测试编写:

  1. TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {
  2. EXPECT_FALSE(table_->IsPrime(-5));
  3. EXPECT_FALSE(table_->IsPrime(0));
  4. EXPECT_FALSE(table_->IsPrime(1));
  5. EXPECT_FALSE(table_->IsPrime(4));
  6. EXPECT_FALSE(table_->IsPrime(6));
  7. EXPECT_FALSE(table_->IsPrime(100));
  8. }
  9. TEST_P(PrimeTableTest, ReturnsTrueForPrimes) {
  10. EXPECT_TRUE(table_->IsPrime(2));
  11. EXPECT_TRUE(table_->IsPrime(3));
  12. EXPECT_TRUE(table_->IsPrime(5));
  13. EXPECT_TRUE(table_->IsPrime(7));
  14. EXPECT_TRUE(table_->IsPrime(11));
  15. EXPECT_TRUE(table_->IsPrime(131));
  16. }
  17. TEST_P(PrimeTableTest, CanGetNextPrime) {
  18. EXPECT_EQ(2, table_->GetNextPrime(0));
  19. EXPECT_EQ(3, table_->GetNextPrime(2));
  20. EXPECT_EQ(5, table_->GetNextPrime(3));
  21. EXPECT_EQ(7, table_->GetNextPrime(5));
  22. EXPECT_EQ(11, table_->GetNextPrime(7));
  23. EXPECT_EQ(131, table_->GetNextPrime(128));
  24. }
  25. #define INSTANTIATE_TEST_SUITE_P INSTANTIATE_TEST_CASE_P
  26. INSTANTIATE_TEST_SUITE_P(MeaningfulTestParameters, PrimeTableTest,
  27. Combine(Bool(), Values(1, 10)));

共计3个测试case,测试名为MeaningfulTestParameters,输入的参数是一个comine类,生成正交参数集合:

  1. Combine(Bool(), Values(1, 10)));
  2. // Combine() allows the user to combine two or more sequences to produce
  3. // values of a Cartesian product of those sequences' elements.
  4. /*
  5. |--Bool--|--------- Value---------|
  6. | | 1 | 10 |
  7. | true | (true,1) | (true,10) |
  8. | false | (false,1) | (false,10) |
  9. */

本例有3个测试,参数正交后是4组参数,每组参数运行一次测试,所以输出12个测试结果。运行截图如下。

 

尊重技术文章,转载请注明!

Google单元测试框架gtest之官方sample笔记3–值参数化测试 

https://www.cnblogs.com/pingwen/p/14476324.html

版权声明:本文为pingwen原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/pingwen/p/14476324.html