gpt4 book ai didi

c++ - Gmock 参数化测试运行两次

转载 作者:行者123 更新时间:2023-11-30 05:00:41 26 4
gpt4 key购买 nike

示例代码:

struct MyFixture: public ::testing::Test
{
};

template <typename param>
struct MyFixtureWithParam: public MyFixture, public ::testing::WithParamInterface<param>
{
};

using MyFixtureWithNumber = MyFixtureWithParam<int>;

TEST_P(MyFixtureWithNumber, Test1)
{
std::cout << "Test1 with param: " << GetParam() << std::endl;
}

INSTANTIATE_TEST_CASE_P(Test1, MyFixtureWithNumber, ::testing::Values(0,3));

TEST_P(MyFixtureWithNumber, Test2)
{
std::cout << "Test2 with param: " << GetParam() << std::endl;
}

INSTANTIATE_TEST_CASE_P(Test2, MyFixtureWithNumber, ::testing::Values(5, 7));

我希望获得 4 个测试(Test1 有 2 个实例,Test2 有 2 个实例),但运行了 8 个测试。为什么?如何解决?

最佳答案

INSTANTIATE_TEST_CASE_P(prefix, fixture, generator)

注册要为使用相同 fixture 类型的每个 参数化测试用例运行的测试。 prefix 仅用于构建参数化测试的名称,这些名称出现在输出中并且可以与 --gtest-filter 命令行参数一起使用。

因此,如果您希望 Test1Test2 使用不同的参数值集运行,您需要强制它们为不同的夹具类型:

// ...

struct MyFixtureForTest1 : public MyFixtureWithNumber {};
struct MyFixtureForTest2 : public MyFixtureWithNumber {};

TEST_P(MyFixtureForTest1, Test1)
{
std::cout << "Test1 with param: " << GetParam() << std::endl;
}

INSTANTIATE_TEST_CASE_P(SmallValues, MyFixtureForTest1, ::testing::Values(0,3));

TEST_P(MyFixtureForTest2, Test2)
{
std::cout << "Test2 with param: " << GetParam() << std::endl;
}

INSTANTIATE_TEST_CASE_P(LargeValues, MyFixtureForTest2, ::testing::Values(5, 7));

关于c++ - Gmock 参数化测试运行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50659375/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com