我想将参数化测试与类型化测试混合使用。这是我的尝试:
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"));
Gtest是否包含一些混合了TYPED_TEST和TEST_P的宏,或者上述代码是实现我的目标的唯一方法?
答案 0 :(得分:1)
据我所知可能没有这样的宏,但是有如下一种可能的方法。
基本思路
让我们定义以下结构a
,b
和Case
。
这里的a::str
和b::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;
}
};
在单个测试逻辑中测试类型X
和Y
的所有组合以及字符串参数的基本思想是使用此结构Case
来指定测试类型和参数,如下所示:
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();
}
改进
现在我们的问题是如何改善类型和字符串参数的所有组合的构造。
我已经回答了almost same problem。
在当前情况下,所有可能的{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>
其中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;
应用此post的答案,我们可以将此元组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();
}