我想要实现的是一个参数化测试TEST_P(MyFixtureClass, DoStuff)
,我可以用它来测试不同的值。虽然所述值不应该是常量,例如通常传递给INSTANTIATE_TEST_CASE_P
的值。而且,我想在其他一些夹具类中使用这些值 - 理想情况下。
似乎没有任何东西,其中包括在创建参数化测试时使用字段而不是静态值。 official documentation似乎也无法掩盖这一点 - 遗憾的是。
但是为了避免在这个问题中引入XY问题,这里是等效的伪代码:
参数化夹具MyFixture
:
struct MyFixture : OtherFixture, ::testing::WithParamInterface<float>
{
float a;
void SetUp() override
{
a = GetParam();
}
};
OtherFixture
看起来像这样:
struct OtherFixture : testing::Test
{
float a;
float b;
float c;
void SetUp() override
{
a = CalculateSomeFloat();
b = CalculateSomeFloat();
c = CalculateSomeFloat();
}
};
测试用例如下:
// This here is the key aspect.
// Basically, I do not want to write a bunch of tests for a, b and c.
// Rather, I'd just test all 3 with this one.
TEST_P(MyFixture, DoStuff)
{
...bunch of tests
}
最后,我们将实例化参数化测试:
INSTANTIATE_TEST_CASE_P(MyFloatTesting, MyFixture, ::testing::Values(
OtherFixture::a, OtherFixture::b, OtherFixture::c
));
显然,OtherFixture::a
是不合适的,但它说明了我想要在一个继承的fixture类(或任何夹具类)中引用一个字段的位置。
有没有办法用gtest实现这个目标?我不一定需要使用参数化测试。我可以简单地避免为不同的对象编写相同的测试。
任何建议都非常感谢!
答案 0 :(得分:3)
我认为您需要使用::testing::Combine
。
并将参数从float
更改为std::tuple<float, float OtherFixture::*>
。
using OtherFixtureMemberAndValue = std::tuple<float, float OtherFixture::*>;
struct MyFixture : OtherFixture, ::testing::WithParamInterface<OtherFixtureMemberAndValue>
{
float a = std::get<0>(GetParam());
auto& memberToTest()
{
return this->*std::get<1>(GetParam());
}
};
要定义参数集,请使用以下方法:
const auto membersToTest = testing::Values(
&OtherFixture::a,
&OtherFixture::b,
&OtherFixture::c
);
const auto floatValuesToTest = testing::Values(
2.1,
3.2
// ...
);
INSTANTIATE_TEST_CASE_P(AllMembers,
MyFixture,
testing::Combine(floatValuesToTest, membersToTest));
然后,您可以针对OtherFixture
的成员编写通用测试:
TEST_P(MyFixture, test)
{
ASSERT_EQ(a, memberToTest());
}
我还建议您为float OtherFixture::*
撰写PrintTo
:
void PrintTo(float OtherFixture::*member, std::ostream* os)
{
if (member == &OtherFixture::a)
*os << "&OtherFixture::a";
else if (member == &OtherFixture::b)
*os << "&OtherFixture::b";
else if (member == &OtherFixture::c)
*os << "&OtherFixture::c";
else
*os << "&OtherFixture::? = " << member;
}
通过这种方式,您可以在出现故障时获得良好的信息:
[FAILED] AllMembers / MyFixture.test / 5,其中GetParam()=(3.2, &安培; OtherFixture :: c)中
与没有PrintTo的讨厌,毫无意义的消息相比:
[FAILED] AllMembers / MyFixture.test / 5,其中GetParam()=(3.2, 4字节对象&lt; 10-00 00-00&gt;)