GTest, parameterize test case only

淺唱寂寞╮ 提交于 2021-02-07 09:56:54

问题


I want to apply a parameterized test and have the following fixture:

class MyTest: public ::testing::TestWithParam<float> {...};

I want to set up two parameterized test cases, one where small parameters fail, but larger succeed.

TEST_P(MyTest, smallParamsFail)
{
    auto param = GetParam();
    EXPECT_EQ(true, param<1);
}


TEST_P(MyTest, largeParamsSucceed)
{
    auto param = GetParam();
    EXPECT_EQ(true, param>1);
}

I want now to start testcase smallParamsFail with values smaller one, the other with values larger one. Unfortunately, I can only parameterize the whole test like this:

INSTANTIATE_TEST_CASE_P(
    testLargeParams,
    MyTest,
    ::testing::Values(2.0f, 3.14f));

That will execute both test cases and obviously crash on case smallParamsFail. What I need is an instantiation of MyTest with the parameters 2.0f, 3.14f only on test case largeParamsSucceed. And then initiate the other case in same manner with appropriate parameters.


回答1:


It's not possible in GTest, as already mentioned in the comment by yksisarvinen.

I will use the following workaround

class MyTestBase: public ::testing::TestWithParam<float> {
protected:
    MyTestBase() {
        // whatever
    }
    // whatever
    int someParam_;
};

class MyTestForSmall: public MyTestBase {
};

class MyTestForBig: public MyTestBase {
};

TEST_P(MyTestForSmall, smallParamsFail) {
    auto param = GetParam();
    EXPECT_EQ(true, param<1);
}

TEST_P(MyTestForBig, largeParamsSucceed) {
    auto param = GetParam();
    EXPECT_EQ(true, param>1);
}

TEST_P(MyTestBase, someGeneralTest) {
    auto param = GetParam();
    EXPECT_TRUE(true);
}

INSTANTIATE_TEST_CASE_P(
    testSmallParams,
    MyTestForSmall,
    ::testing::Values(0.1, 0.2));

INSTANTIATE_TEST_CASE_P(
    testLargeParams,
    MyTestForBig,
    ::testing::Values(2.0f, 3.14f));

INSTANTIATE_TEST_CASE_P(
    allKindsOfParams,
    MyTestBase,
    ::testing::Values(0.1, 0.2, 2.0f, 3.14f));

In the base class you can setup all necessary environment and use child classes only for test parameters separation.

Possible output:

[----------] 2 tests from testSmallParams/MyTestForSmall
[ RUN      ] testSmallParams/MyTestForSmall.smallParamsFail/0
[       OK ] testSmallParams/MyTestForSmall.smallParamsFail/0 (0 ms)
[ RUN      ] testSmallParams/MyTestForSmall.smallParamsFail/1
[       OK ] testSmallParams/MyTestForSmall.smallParamsFail/1 (0 ms)
[----------] 2 tests from testSmallParams/MyTestForSmall (0 ms total)

[----------] 2 tests from testLargeParams/MyTestForBig
[ RUN      ] testLargeParams/MyTestForBig.largeParamsSucceed/0
[       OK ] testLargeParams/MyTestForBig.largeParamsSucceed/0 (0 ms)
[ RUN      ] testLargeParams/MyTestForBig.largeParamsSucceed/1
[       OK ] testLargeParams/MyTestForBig.largeParamsSucceed/1 (0 ms)
[----------] 2 tests from testLargeParams/MyTestForBig (0 ms total)

[----------] 4 tests from allKindsOfParams/MyTestBase
[ RUN      ] allKindsOfParams/MyTestBase.someGeneralTest/0
[       OK ] allKindsOfParams/MyTestBase.someGeneralTest/0 (0 ms)
[ RUN      ] allKindsOfParams/MyTestBase.someGeneralTest/1
[       OK ] allKindsOfParams/MyTestBase.someGeneralTest/1 (0 ms)
[ RUN      ] allKindsOfParams/MyTestBase.someGeneralTest/2
[       OK ] allKindsOfParams/MyTestBase.someGeneralTest/2 (0 ms)
[ RUN      ] allKindsOfParams/MyTestBase.someGeneralTest/3
[       OK ] allKindsOfParams/MyTestBase.someGeneralTest/3 (0 ms)
[----------] 4 tests from allKindsOfParams/MyTestBase (0 ms total)


来源:https://stackoverflow.com/questions/52666475/gtest-parameterize-test-case-only

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!