如何在 Google 测试 (gtest) 中使用夹具成员值 运行 参数化测试?

How to run Parameterized Tests with fixture member values in Google Test (gtest)?

我想要实现的是参数化测试TEST_P(MyFixtureClass, DoStuff),我可以用它来测试不同的值。尽管所说的值不应该是常量,就像通常传递给 INSTANTIATE_TEST_CASE_P 的那些值一样。此外,我想在其他一些固定装置中使用这些值 class - 理想情况下。

似乎没有任何东西可以涵盖在创建参数化测试时使用字段而不是静态值。 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 是不合适的,但它说明了我想在继承的夹具 class(或任何夹具 class 中引用字段的位置) .


那么有什么方法可以用gtest来实现吗?我不一定需要使用参数化测试。简单地避免必须为不同的对象编写相同的测试对我来说很好。


非常感谢任何建议!

我认为你需要使用 ::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, where GetParam() = (3.2, &OtherFixture::c)

与讨厌的、毫无意义的消息相比w/o PrintTo:

[ FAILED ] AllMembers/MyFixture.test/5, where GetParam() = (3.2, 4-byte object <10-00 00-00>)