值參數不限定類型,也能夠是類的引用,這就能夠實現對類接口的測試,一個基類能夠有多個繼承類,那麼能夠測試不一樣的子類功能,可是隻須要寫一個測試用例,而後使用參數列表實現對每一個子類的測試。html
算法
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)。ide
參數值序列生成函數 | 含義 |
---|---|
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類,生成正交參數集合:this
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個測試結果。運行截圖以下。spa
尊重技術文章,轉載請註明!code
Google單元測試框架gtest之官方sample筆記3--值參數化測試