Gmock参数化测试运行两次

时间:2018-06-02 17:20:10

标签: c++ googletest gmock

以下是一个示例代码:

private static bool PermissionComparison(GuildPermissions targetGuildPerms, GuildPermissions userGuildPerms)
{
    //True if the target has a higher role.
    bool targetHasHigherPerms = false;
    //If the user is not admin but target is.
    if(!userGuildPerms.Administrator && targetGuildPerms.Administrator) {
        //The target has higher permission than the user.
        targetHasHigherPerms = true;
    } else if(!userGuildPerms.ManageGuild && targetGuildPerms.ManageGuild) {
        targetHasHigherPerms = true;
    } else if(!userGuildPerms.ManageChannels && targetGuildPerms.ManageChannels) {
        targetHasHigherPerms = true;
    } else if(!userGuildPerms.BanMembers && targetGuildPerms.BanMembers) {
        targetHasHigherPerms = true;
    } else if(!userGuildPerms.KickMembers && targetGuildPerms.KickMembers) {
        targetHasHigherPerms = true;
    }

    return targetHasHigherPerms;
}

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

1 个答案:

答案 0 :(得分:1)

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));