GTest - 不同类型的参数化测试

Irb*_*bis 5 c++ googletest googlemock

我想将参数化测试与类型化测试混合在一起。这是我的尝试:

struct X {};

struct Y {};

template <typename T>
struct MyTestFixture: public ::testing::Test
{
    T t;
};

template <typename T, typename Param>
struct MyTestFixtureWithParam : public MyTestFixture<T>, 
                                public ::testing::WithParamInterface<Param>
{
};

using MyTestFixtureWithXandString = MyTestFixtureWithParam<X, std::string>;

TEST_P(MyTestFixtureWithXandString, Test1)
{
}

INSTANTIATE_TEST_CASE_P(Name, MyTestFixtureWithXandString, 
                        ::testing::Values("a", "b"));

using MyTestFixtureWithYandString = MyTestFixtureWithParam<Y, std::string>;

TEST_P(MyTestFixtureWithYandString, Test1)
{

}

INSTANTIATE_TEST_CASE_P(Name, MyTestFixtureWithYandString, 
                        ::testing::Values("a", "b"));
Run Code Online (Sandbox Code Playgroud)

Gtest 是否包含一些混合 TYPED_TEST 和 TEST_P 的宏,或者上面的代码是实现我的目标的唯一方法?

Hir*_*oki 2

据我所知,可能没有这样的宏,但有一种可能的方法可以做到这一点,如下所示。


基本思想

让我们定义以下结构体a,bCase。这里a::strb::str是我们要测试的参数:

// types
struct X {};
struct Y {};

// "parameters"
struct a { static constexpr char str[] = "a"; };
struct b { static constexpr char str[] = "b"; };

template<class T, class P>
struct Case
{
    using type = T;

    static std::string GetParam()
    {
        return P::str;
    }
};
Run Code Online (Sandbox Code Playgroud)

在单个测试逻辑中测试类型X和字符串参数的所有组合的基本思想是使用此结构来指定测试类型和参数,如下所示:YCase

using TestTypes = ::testing::Types<Case<X, a>, Case<X, b>, Case<Y, a>, Case<Y, b>>;

template <class T>
class MyTestFixture: public ::testing::Test {};

TYPED_TEST_CASE(MyTestFixture, TestTypes);

TYPED_TEST(MyTestFixture, Test12) 
{
    // X or Y
    TypeParam::type t;

    // "a" or "b"
    const std::string str = TypeParam::GetParam();
}
Run Code Online (Sandbox Code Playgroud)

改进

现在我们的问题是如何改进类型和字符串参数的所有组合的构造。我已经回答了几乎相同的问题。在当前情况下,所有可能的{X, Y}{a, b}配对都由一维整数标记,0,1,2,3如下所示:

0 -> (0/2, 0%2) = (0,0) -> Case<X, a>
1 -> (1/2, 1%2) = (0,1) -> Case<X, b>
2 -> (2/2, 2%2) = (1,0) -> Case<Y, a>
3 -> (3/2, 3%2) = (1,1) -> Case<Y, b>
Run Code Online (Sandbox Code Playgroud)

其中2的大小为{a, b}. 编写使用该算法进行所有可能组合的函数很简单,如下所示。例如,Combinations_t<std::tuple<X, Y>, a, b>等于以下类型std::tuple<Case<X,a>, Case<X,b>, Case<Y,a>, Case<Y,b>>

现场演示

template<class TupleType, class TupleParam, std::size_t I>
struct make_case
{
    static constexpr std::size_t N = std::tuple_size<TupleParam>::value;

    using type = Case<typename std::tuple_element<I / N, TupleType >::type,
                      typename std::tuple_element<I % N, TupleParam>::type>;
};

template <class T1, class T2, class Is>
struct make_combinations;

template <class TupleType, class TupleParam, std::size_t... Is>
struct make_combinations<TupleType, TupleParam, std::index_sequence<Is...>>
{
    using tuples = std::tuple<typename make_case<TupleType, TupleParam, Is>::type...>;
};

template<class TupleTypes, class... Params>
using Combinations_t = typename make_combinations
                       <TupleTypes,
                        std::tuple<Params...>,
                        std::make_index_sequence<(std::tuple_size<TupleTypes>::value)*(sizeof...(Params))>>
                     ::tuples;
Run Code Online (Sandbox Code Playgroud)

应用这篇文章的答案,我们可以将此元组划分Combinations_t<...>为类型列表,在这里我应用 Nawaz 的简单类型。最后,所有测试可以按如下方式完成:

template <class T>
class MyTestFixture : public ::testing::Test {};

template<class T>
struct Test;

template<class ...T>
struct Test<std::tuple<T...>>
{
    using Types = ::testing::Types<T...>;
};

using TestTypes = Test<Combinations_t<std::tuple<X, Y>, a, b>>::Types;

TYPED_TEST_CASE(MyTestFixture, TestTypes);

TYPED_TEST(MyTestFixture, Test12)
{
    // X or Y
    TypeParam::type t;

    // "a" or "b"
    const std::string str = TypeParam::GetParam();
}
Run Code Online (Sandbox Code Playgroud)