Google单元测试框架gtest之官方sample笔记3--值参数化测试
创始人
2024-03-06 05:12:51
0

1.7 sample7--接口测试

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

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

using ::testing::TestWithParam;
using ::testing::Values;
​
typedef PrimeTable* CreatePrimeTableFunc();
​
PrimeTable* CreateOnTheFlyPrimeTable() {return new OnTheFlyPrimeTable();
}
​
template 
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 {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)生成 g1g2, ..., gN的笛卡尔积,其中g1g2, ..., gN均为参数值序列生成函数(要求C++0x的

代码实现

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);elsereturn 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 > {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为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个测试结果。运行截图如下。

相关内容

热门资讯

QQ群晚会开幕词 QQ群晚会开幕词  QQ群晚会开幕词    尊贵的各位嘉宾,群主,各位管理,群友们大家晚上好:   ...
主持谢幕词 主持谢幕词范本  篇一:主持谢幕词  愿一切荣耀、尊贵、颂赞都归给至高上帝!  都说“台上一分钟,台...
圣诞节联欢晚会主持词 圣诞节联欢晚会主持词  主持词的写作需要将主题贯穿于所有节目之中。在一步步向前发展的社会中,主持词与...
元宵节的主持词 元宵节的主持词  一般在节日的时候,都是会举行一些活动的,尤其是在元宵节这个大型的节日。下面是小编为...
初中生晨会主持词 初中生晨会主持词(通用5篇)  主持词要尽量增加文化内涵、寓教于乐,不断提高观众的文化知识和素养。现...
晨会主持词开场白   开晨会是公司职场管理的规章制度,那么公司晨会主持如何开场白好呢?以下是小编为大家搜集整理提供到的...
企业年会主持词 企业年会主持词  主持词是主持人在台上表演的灵魂之所在。在人们越来越多的参与各种活动的今天,主持词是...
企业晚会的主持词 企业晚会的主持词  借鉴诗词和散文诗是主持词的一种写作手法。在人们越来越多的参与各种活动的今天,主持...
年终总结会主持词 2021年终总结会主持词范文(精选13篇)  契合现场环境的主持词能给集会带来双倍的效果。现今社会在...
半台词爆笑 三句半台词大全爆笑  三句半是一种中国民间群众传统曲艺表演形式,下面是为带大家整理的爆笑的'三句半台...
三八妇女节活动主持词 三八妇女节活动主持词3篇  三月的春风拂过我们脚下的土地,三月的惊雷敲响了我们奋进的汽笛,三月我们迎...
文艺晚会主持人主持词 文艺晚会主持人主持词(精选10篇)  主持词是各种演出活动和集会中主持人串联节目的串联词。在当下这个...
校园文艺晚会结束语 下面文艺晚会结束语是小编为你们寻找的,希望你们会喜欢喔文艺晚会结束语一女1:最明快的,莫过于一年一度...
红色经典诵读主持词 红色经典诵读主持词红色经典诵读主持词尊敬的各位领导、敬爱的老师、亲爱的同学们 :大家好!甲:今天的阳...
答谢会主持词 答谢会主持词15篇  主持词要根据活动对象的不同去设置不同的主持词。随着中国在不断地进步,主持人在活...
年会游戏主持词 年会游戏主持词  主持词没有固定的格式,他的最大特点就是富有个性。在人们积极参与各种活动的今天,主持...
《我是女王》经典台词及剧情介... 《我是女王》经典台词及剧情介绍  一、经典台词  一个偶尔会消失的男人,总有一天会永远的消失。  女...
追梦的主持串词 关于追梦的主持串词  篇一:梦想串词  各位老师,大家好:  又到了一个追梦的季节。春之漫妙、夏之热...
生日宴会精彩致辞 生日宴会精彩致辞(精选5篇)  在日常学习、工作抑或是生活中,大家都不可避免地会接触到致辞吧,致辞是...
暨迎元旦合唱比赛主持词 暨迎元旦合唱比赛主持词  主持词没有固定的格式,他的最大特点就是富有个性。在当下这个社会中,很多场合...