gtest中的参数化测试允许您使用不同的参数测试代码,而无需编写同一测试的多个副本。在这里看到的。
我已经看到了传递值的例子,std::结对,std::tuple等等。
但我不知道如何将数组/初始化器_list传递到测试中。
预期会发生这样的事情:
INSTANTIATE_TEST_SUITE_P(Sample, FooTest,
testing::Values({1,23,53},{534,34,456));有可能吗?如果是,怎么做?
发布于 2020-08-12 15:08:27
您可以将任何类型作为参数传递。当您从类模板WithParamInterface (或TestWithParam)继承测试夹具a时,可以提供参数类型:
class FooTest: public TestWithParam<std::array<int, 3>>
//class FooTest: public TestWithParam<std::vector<int>>
//class FooTest: public TestWithParam<std::initializer_list<int>> //I'm not sure if this is a good idea, initializer_list has weird lifetime management
{};
INSTANTIATE_TEST_SUITE_P(Sample, FooTest,
testing::Values(std::array<int, 3>{1,23,53},
std::array<int, 3>{534,34,456});在网上看。
您不能使用裸大括号-init列表并让编译器推断类型,因为::testing::Values()接受模板参数,而编译器不知道模板参数应该变成什么类型。
假设我们有class BarTest: public TestWithParam<std::string>。对于::testing::Values,我们可以传递实际的std::string对象::testing::Values(std::string{"asdf"}, "qwer"s)或隐式转换为std::string的对象,就像string文本:::testing::Values("zxcv")一样。后者将推断类型为const char*,而实际的std::string在GoogleTest代码中构造得更深。
https://stackoverflow.com/questions/63379338
复制相似问题