Can Googletest value-parameterized with multiple, different types of parameters match mbUnit flexibility?

前端 未结 2 2097
孤城傲影
孤城傲影 2020-12-28 15:47

I\'d like to write C++ Google tests which can use value-parameterized tests with multiple parameters of different data types, ideally matching the complexity of the followin

2条回答
  •  太阳男子
    2020-12-28 16:38

    An alternative to using a custom structure as the parameter is to use the parameter generator ::testing::Combine(g1, g2, ..., gn). This generator allows you to combine the other parameter generators into a set of parameters with a type std::tuple that has a template type that matches the types of the values provided.

    Note that this generator produces the Cartesian product of the values provided. That means that every possible ordered tuple will be created. I believe the original question is asking for a strict array of parameters with the provided values, which this does not support. If you need to have an array of strict parameters, you could use a tuple with the parameter generator ::testing::Values(v1, v2, ..., vN) where each value is a separate tuple.

    Example:

    #include 
    #include 
    
    class MyTestSuite : 
      public testing::TestWithParam>
    {
    
    };
    
    TEST_P(MyTestSuite, TestThatThing)
    {
      functionUnderTest(std::get<0>(GetParam()), 
                        std::get<1>(GetParam()), 
                        std::get<2>(GetParam()));
      . . .
    }
    
    INSTANTIATE_TEST_SUITE_P(
      MyTestGroup,
      MyTestSuite,
      ::testing::Combine(
        ::testing::Values("FirstString1", "FirstString2"),
        ::testing::Values("SecondString1", "SecondString2"),
        ::testing::Range(10, 13)));
    
    INSTANTIATE_TEST_SUITE_P(
      MyOtherTestGroupThatUsesStrictParameters,
      MyTestSuite,
      ::testing::Values(
        {"FirstString1", "SecondString1", 10},
        {"FirstString2", "SecondString2", 32},
        {"FirstString3", "SecondString3", 75}));
    

    In the above example, the parameters created for MyTestGroup would look like the following:

    [
      {"FirstString1", "SecondString1", 10},
      {"FirstString1", "SecondString1", 11},
      {"FirstString1", "SecondString1", 12},
      {"FirstString1", "SecondString2", 10},
      {"FirstString1", "SecondString2", 11},
      {"FirstString1", "SecondString2", 12},
      {"FirstString2", "SecondString1", 10},
      {"FirstString2", "SecondString1", 11},
      {"FirstString2", "SecondString1", 12},
      {"FirstString2", "SecondString2", 10},
      {"FirstString2", "SecondString2", 11},
      {"FirstString2", "SecondString2", 12}
    ]
    

    Refer to the GoogleTest documentation for further details. (Accessed on 12/17/2019)

提交回复
热议问题