这就是我所拥有的:
/* Can't change 'base' struct. */
struct base {
public:
template<typename T>
void printVal() {
std::cout << T::x << std::endl;
}
};
struct testFixture
: public base
, public ::testing::Test {
using base::printVal;
};
TEST_F(testFixture, testF) {
printVal<A>();
printVal<B>();
printVal<C>();
}
哪个工作正常。但是,我想使用类型化测试,因此代码看起来更像是这样:
/* Same base class as above. */
struct testFixture
: public base
, public ::testing::Test {
using base::printVal;
};
typedef ::testing::Types<A, B, C> MyTypes;
TYPED_TEST_CASE(testFixture, MyTypes);
TYPED_TEST(testFixture, typedTest) {
printVal<TypeParam>();
}
当然,因为&#39; testFixture&#39;不是模板化的结构/类,这不起作用,我得到这个错误:
test2.cpp:31:12: error: unknown template name 'testFixture'
TYPED_TEST(testFixture, typedTest) {
有没有办法可以使用googletest获得此类功能?
答案 0 :(得分:2)
想出来。
'模板化'夹具:
template <typename T>
struct testFixture
: public base
, public ::testing::Test {
using base::printVal;
void printVal() {
base::printVal<T>();
}
};
然后照常进行:
typedef ::testing::Types<A, B, C> MyTypes;
TYPED_TEST_CASE(testFixture, MyTypes);
TYPED_TEST(testFixture, typedTest) {
this->template printVal<TypeParam>();
}