1. 程式人生 > >Google單元測試框架gtest之官方sample筆記3--值引數化測試

Google單元測試框架gtest之官方sample筆記3--值引數化測試

1.7 sample7--介面測試

值引數不限定型別,也可以是類的引用,這就可以實現對類介面的測試,一個基類可以有多個繼承類,那麼可以測試不同的子類功能,但是隻需要寫一個測試用例,然後使用引數列表實現對每個子類的測試。

使用值引數測試法去測試多個實現了相同介面(類)的共同屬性(又叫做介面測試)

using ::testing::TestWithParam;
using ::testing::Values;
​
typedef PrimeTable* CreatePrimeTableFunc();
​
PrimeTable* CreateOnTheFlyPrimeTable() {
  return new OnTheFlyPrimeTable();
}
​
template <size_t max_precalculated>
PrimeTable* CreatePreCalculatedPrimeTable() {
  return new PreCalculatedPrimeTable(max_precalculated);
}
​
// Inside the test body, fixture constructor, SetUp(), and TearDown() you
// can refer to the test parameter by GetParam().  In this case, the test
// parameter is a factory function which we call in fixture's SetUp() to
// create and store an instance of PrimeTable.
class PrimeTableTestSmpl7 : public TestWithParam<CreatePrimeTableFunc*> {
 public:
  ~PrimeTableTestSmpl7() override { delete table_; }
  void SetUp() override { table_ = (*GetParam())(); }
  void TearDown() override {
    delete table_;
    table_ = nullptr;
  }
​
 protected:
  PrimeTable* table_;
};
​
TEST_P(PrimeTableTestSmpl7, ReturnsFalseForNonPrimes) {
  EXPECT_FALSE(table_->IsPrime(-5));
  EXPECT_FALSE(table_->IsPrime(0));
  EXPECT_FALSE(table_->IsPrime(1));
  EXPECT_FALSE(table_->IsPrime(4));
  EXPECT_FALSE(table_->IsPrime(6));
  EXPECT_FALSE(table_->IsPrime(100));
}
​
TEST_P(PrimeTableTestSmpl7, ReturnsTrueForPrimes) {
  EXPECT_TRUE(table_->IsPrime(2));
  EXPECT_TRUE(table_->IsPrime(3));
  EXPECT_TRUE(table_->IsPrime(5));
  EXPECT_TRUE(table_->IsPrime(7));
  EXPECT_TRUE(table_->IsPrime(11));
  EXPECT_TRUE(table_->IsPrime(131));
}
 
TEST_P(PrimeTableTestSmpl7, CanGetNextPrime) {
  EXPECT_EQ(2, table_->GetNextPrime(1));
  EXPECT_EQ(3, table_->GetNextPrime(2));
  EXPECT_EQ(5, table_->GetNextPrime(3));
  EXPECT_EQ(7, table_->GetNextPrime(5));
  EXPECT_EQ(11, table_->GetNextPrime(7));
  EXPECT_EQ(131, table_->GetNextPrime(128));
}
​
// In order to run value-parameterized tests, you need to instantiate them,
// or bind them to a list of values which will be used as test parameters.
// You can instantiate them in a different translation module, or even
// instantiate them several times.
//
// Here, we instantiate our tests with a list of two PrimeTable object
// factory functions:
#define INSTANTIATE_TEST_SUITE_P INSTANTIATE_TEST_CASE_P
INSTANTIATE_TEST_SUITE_P(OnTheFlyAndPreCalculated, PrimeTableTestSmpl7,
                         Values(&CreateOnTheFlyPrimeTable,
                                &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>

程式碼實現

class HybridPrimeTable : public PrimeTable {
 public:
  HybridPrimeTable(bool force_on_the_fly, int max_precalculated)
      : on_the_fly_impl_(new OnTheFlyPrimeTable),
        precalc_impl_(force_on_the_fly
                          ? nullptr
                          : new PreCalculatedPrimeTable(max_precalculated)),
        max_precalculated_(max_precalculated) {}
  ~HybridPrimeTable() override {
    delete on_the_fly_impl_;
    delete precalc_impl_;
  }
​
  bool IsPrime(int n) const override {
    if (precalc_impl_ != nullptr && n < max_precalculated_)
      return precalc_impl_->IsPrime(n);
    else
      return on_the_fly_impl_->IsPrime(n);
  }
​
  int GetNextPrime(int p) const override {
    int next_prime = -1;
    if (precalc_impl_ != nullptr && p < max_precalculated_)
      next_prime = precalc_impl_->GetNextPrime(p);
​
    return next_prime != -1 ? next_prime : on_the_fly_impl_->GetNextPrime(p);
  }
​
 private:
  OnTheFlyPrimeTable* on_the_fly_impl_;
  PreCalculatedPrimeTable* precalc_impl_;
  int max_precalculated_;
};
​
using ::testing::TestWithParam;
using ::testing::Bool;
using ::testing::Values;
using ::testing::Combine;
​
// To test all code paths for HybridPrimeTable we must test it with numbers
// both within and outside PreCalculatedPrimeTable's capacity and also with
// PreCalculatedPrimeTable disabled. We do this by defining fixture which will
// accept different combinations of parameters for instantiating a
// HybridPrimeTable instance.
class PrimeTableTest : public TestWithParam< ::std::tuple<bool, int> > {
 protected:
  void SetUp() override {
    bool force_on_the_fly;
    int max_precalculated;
    std::tie(force_on_the_fly, max_precalculated) = GetParam();
    table_ = new HybridPrimeTable(force_on_the_fly, max_precalculated);
  }
  void TearDown() override {
    delete table_;
    table_ = nullptr;
  }
  HybridPrimeTable* table_;
};

PrimeTableTest類繼承於TestWithParam,是測試韌體類。接收引數tuple<bool,int>,如果bool為true時,使用OnTheFlyPrimeTable類的介面,當bool變數為false時,使用PreCalculatedPrimeTable介面測試。

測試編寫:

TEST_P(PrimeTableTest, ReturnsFalseForNonPrimes) {
  EXPECT_FALSE(table_->IsPrime(-5));
  EXPECT_FALSE(table_->IsPrime(0));
  EXPECT_FALSE(table_->IsPrime(1));
  EXPECT_FALSE(table_->IsPrime(4));
  EXPECT_FALSE(table_->IsPrime(6));
  EXPECT_FALSE(table_->IsPrime(100));
}
​
TEST_P(PrimeTableTest, ReturnsTrueForPrimes) {
  EXPECT_TRUE(table_->IsPrime(2));
  EXPECT_TRUE(table_->IsPrime(3));
  EXPECT_TRUE(table_->IsPrime(5));
  EXPECT_TRUE(table_->IsPrime(7));
  EXPECT_TRUE(table_->IsPrime(11));
  EXPECT_TRUE(table_->IsPrime(131));
}
​
TEST_P(PrimeTableTest, CanGetNextPrime) {
  EXPECT_EQ(2, table_->GetNextPrime(0));
  EXPECT_EQ(3, table_->GetNextPrime(2));
  EXPECT_EQ(5, table_->GetNextPrime(3));
  EXPECT_EQ(7, table_->GetNextPrime(5));
  EXPECT_EQ(11, table_->GetNextPrime(7));
  EXPECT_EQ(131, table_->GetNextPrime(128));
}
​
​
#define INSTANTIATE_TEST_SUITE_P INSTANTIATE_TEST_CASE_P
INSTANTIATE_TEST_SUITE_P(MeaningfulTestParameters, PrimeTableTest,
                         Combine(Bool(), Values(1, 10)));

共計3個測試case,測試名為MeaningfulTestParameters,輸入的引數是一個comine類,生成正交引數集合:

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

本例有3個測試,引數正交後是4組引數,每組引數執行一次測試,所以輸出12個測試結果。執行截圖如下。

 

尊重技術文章,轉載請註明!

Google單元測試框架gtest之官方sample筆記3--值引數化測試 

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