Google Test Sample08:接口的多个实施例同时使用的测试场景
一、环境信息
- Visual Studio 2019
- Windows 10
- 前导知识:Google Test自带示例七的学习总结
二、调试中遇到的错误
- 问题描述:编译可以正常通过,但是进行单元测试时:程序崩溃,VS弹出abort() has been called的提示
- 排查方法:缩小执行代码的范围 并 编写可以正常运行测试的代码,随后进行比对
- 问题原因:相同测试工程下,sample08中定义的test fixture名字 和 sample07中定义的名字相同,从而触发了上述报错。将test fixture更名后,问题得到了规避,但是根体原因还是不清楚
三、Google Test Sample08
1. 示例概述
1.1 待测试对象HybridPrimeTable同时包含接口的两种实施例:OnTheFlyPrimeTable、PreCalculatedPrimeTable,HybridPrimeTable所要达成的目标是:在不同的环境下,使用最佳的实施例
1.2 测试对象HybridPrimeTable的具体实现代码如下
class HybridPrimeTable : public PrimeTable //Hybrid
{
private:
OnTheFlyPrimeTable* on_the_fly_impl_;
PreCalculatedPrimeTable* precalc_impl_;
int max_precalculated_;
public:
HybridPrimeTable(bool force_on_the_fly, int max_precalculated) : //construct function
on_the_fly_impl_(new OnTheFlyPrimeTable), // on_the_fly_impl_=new OnTheFlyPrimeTable
precalc_impl_(force_on_the_fly ? nullptr : new PreCalculatedPrimeTable(max_precalculated)),//force_on_the_fly 是否强制使用实施例OnTheFlyPrimeTable
max_precalculated_(max_precalculated) {}
~HybridPrimeTable() override //deconstruct function
{
delete on_the_fly_impl_;
delete precalc_impl_;
}
bool IsPrime(int n) const override
{
if (precalc_impl_ != nullptr && n < max_precalculated_)//实施例PreCalculatedPrimeTable有效
return precalc_impl_->IsPrime(n);
else //实施例PreCalculatedPrimeTable无效,只能使用实施例OnTheFlyPrimeTable
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);
}
};
2. TestWithParm + Combine()
2.1 类testing::TestWithParm的基础使用共分三步:
(1) 定义test fixture:test fixture 继承自 类testing::TestWithParam,继承时需要注明参数类型,示例中是元组 tuple<bool, int> ,用于处理同时接收多个参数
(2) 使用宏TEST_P编写具体的测试用例:TEST_P(TestFixtureName,TestCaseName)
(3) 使用INSTANTIATE_TEST_CASE_P(TestSuiteName, TestFixtureName,Combine(Bool(), testing::Values(1, 10))); 通过Combine( )函数可以生成所需的参数组合、覆盖代码的所有分支,每一个组合作为参数传入,传入的参数通过GetParam()接收并完成赋值
2.2 每一条测试用例执行前,会先运行test fixture中的SetUp()生成测试需要的实施例;测试用例结束后,调用test fixture中的TearDown()释放内存
2.3 Combine( )产生的每种组合均会在宏TEST_P()定义的测试用例中执行一次(可以参考执行结果进行理解)
class PrimeTableTest02 : public TestWithParam< tuple<bool, int> > // tuple定义的元组包含了bool和int两种数据类型 ::std::tuple<bool, int>
{
protected:
HybridPrimeTable* table_;
void SetUp() override
{
bool force_on_the_fly;
int max_precalculated;
tie(force_on_the_fly, max_precalculated) = GetParam(); // tie(x,y)用于接收具体的参数 std::tie()
table_ = new HybridPrimeTable(force_on_the_fly, max_precalculated);
//Combine(Bool(), Values(1, 10)) 产生的参数组合包括:(1)false,1 (2)false,10 (3)true,1 (4)true,10。GetParam()接收这四种组合,
//然后传递给变量 force_on_the_fly=true/false 和 max_precalculated=1/10,这样很好的覆盖了代码的所有分支
}
void TearDown() override
{
delete table_;
table_ = nullptr;
}
};
TEST_P(PrimeTableTest02, ReturnsFalseForNonPrimes){...}
INSTANTIATE_TEST_CASE_P(MeaningfulTestParameters, PrimeTableTest02,Combine(Bool(), testing::Values(1, 10)));
3. 对应的单元测试用例
3.1 用例执行结果如下。可以看到 Combine(Bool(), Values(1, 10)) 产生的四种参数组合:(1)false,1 (2)false,10 (3)true,1 (4)true,10 都已经在TEST_P()定义的测试用例中得到准确执行
3.2 sample08UnitTest.cpp的详细代码及注释如下(已经在vs2019下调试通过)
#include "pch.h"
// This sample shows how to test code relying on some global flag variables. //?
// Combine() helps with generating all possible combinations of such flags, and each test is given one combination as a parameter.
// 通过Combine()函数可以生成所有可能的参数组合,每一个组合作为参数传入到测试用例中
// Combine(Bool(), Values(1, 10)) 产生的参数组合包括:(1)false,1 (2)false,10 (3)true,1 (4)true,10
#include "PrimeTable.h" // Use class definitions to test from this header.
namespace
{
// Suppose we want to introduce a new, improved implementation of PrimeTable which combines speed of PrecalcPrimeTable and versatility of
// OnTheFlyPrimeTable (see prime_tables.h).
// 下面,我们将引入一个改进后的PrimeTable接口的实施例,他兼顾了 实施例PrecalcPrimeTable的速度以及实施例OnTheFlyPrimeTable的多样性
// Inside it instantiates both PrecalcPrimeTable and OnTheFlyPrimeTable and uses the one that is more appropriate under the circumstances.
// 在新的实施例中,他会同时实例化 PrecalcPrimeTable 和 OnTheFlyPrimeTable,在不同的环境下使用最合适的实施例
// But in low memory conditions, it can be told to instantiate without PrecalcPrimeTable instance at all and use only OnTheFlyPrimeTable.
// 但是在低内存环境,他只能使用OnTheFlyPrimeTable实施例
class HybridPrimeTable : public PrimeTable //Hybrid
{
private:
OnTheFlyPrimeTable* on_the_fly_impl_;
PreCalculatedPrimeTable* precalc_impl_;
int max_precalculated_;
public:
HybridPrimeTable(bool force_on_the_fly, int max_precalculated) : //construct function
on_the_fly_impl_(new OnTheFlyPrimeTable), // on_the_fly_impl_=new OnTheFlyPrimeTable
precalc_impl_(force_on_the_fly ? nullptr : new PreCalculatedPrimeTable(max_precalculated)),//force_on_the_fly 是否强制使用实施例OnTheFlyPrimeTable
max_precalculated_(max_precalculated) {}
~HybridPrimeTable() override //deconstruct function
{
delete on_the_fly_impl_;
delete precalc_impl_;
}
bool IsPrime(int n) const override
{
if (precalc_impl_ != nullptr && n < max_precalculated_)//实施例PreCalculatedPrimeTable有效
return precalc_impl_->IsPrime(n);
else //实施例PreCalculatedPrimeTable无效,只能使用实施例OnTheFlyPrimeTable
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);
}
};
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.
// 为了测试实施例HybridPrimeTable的所有代码路径,我们必须覆盖 PreCalculatedPrimeTable生效和失效的场景
// We do this by defining fixture which will accept different combinations of parameters for instantiating a HybridPrimeTable instance.
// 通过定义接收参数组合的test fixture来实例化HybridPrimeTable
class PrimeTableTest02 : public TestWithParam< tuple<bool, int> > // tuple定义的元组包含了bool和int两种数据类型 ::std::tuple<bool, int>
{
protected:
HybridPrimeTable* table_;
void SetUp() override
{
bool force_on_the_fly;
int max_precalculated;
tie(force_on_the_fly, max_precalculated) = GetParam(); // tie(x,y)用于接收具体的参数 std::tie()
table_ = new HybridPrimeTable(force_on_the_fly, max_precalculated);
//Combine(Bool(), Values(1, 10)) 产生的参数组合包括:(1)false,1 (2)false,10 (3)true,1 (4)true,10。GetParam()接收这四种组合,
//然后传递给变量 force_on_the_fly=true/false 和 max_precalculated=1/10,这样很好的覆盖了代码的所有分支
}
void TearDown() override
{
delete table_;
table_ = nullptr;
}
};
TEST_P(PrimeTableTest02, ReturnsFalseForNonPrimes)
{
// Inside the test body, you can refer to the test parameter by GetParam().
// In this case, the test parameter is a PrimeTable interface pointer which we can use directly.
// 在本示例中,测试参数就是实施例HybridPrimeTable的指针 table_ 。 其他情况下,也可以在测试用例中调用GetParam()获取参数。根据情况灵活使用GetParam()
// Please note that you can also save it in the fixture's SetUp() method or constructor and use saved copy in the tests.
// ?
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(PrimeTableTest02, 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(PrimeTableTest02, 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));
}
// 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 parameters.
// We must combine all variations of the boolean flag suppressing PrecalcPrimeTable and some meaningful values for tests.
// 示例中通过列表形成待使用的参数
// We choose a small value (1), and a value that will put some of the tested numbers beyond the capability of the
// PrecalcPrimeTable instance and some inside it (10). Combine will produce all possible combinations.
// 通过Combine()生成参数组合。除了Combine(),这里也用到了Bool() 和 Values(),这些均是testing的函数 testing::Values()
INSTANTIATE_TEST_CASE_P(MeaningfulTestParameters, PrimeTableTest02,Combine(Bool(), testing::Values(1, 10)));
} // namespace