如何获取gtest TYPED_TEST参数类型

Mir*_*pas 4 c++ googletest

我有一些在Windows(Visual Studio 2017)上编写的单元测试,我需要在Linux(GCC 4.9.2-我受此版本困扰...)上移植它们。我为我的问题提供了一个简单的示例,该示例在Windows上可以很好地编译(我认为它不应该像MyParamTypee模板基类的依赖类型那样进行编译),而不能在Linux上进行编译。

例:

#include <gtest/gtest.h>

template<typename T>
struct MyTest : public testing::Test
{
    using MyParamType = T;
};

using MyTypes = testing::Types<int, float>;
TYPED_TEST_CASE(MyTest, MyTypes);

TYPED_TEST(MyTest, MyTestName)
{
    MyParamType param;
} 
Run Code Online (Sandbox Code Playgroud)

在成员函数'virtual void MyTest_MyTestName_Test :: TestBody()'中:错误:在此范围MyParamType参数中未声明'MyParamType';

通过更改为:

TYPED_TEST(MyTest, MyTestName)
{
    typename MyTest<gtest_TypeParam_>::MyParamType param;
}
Run Code Online (Sandbox Code Playgroud)

代码可以编译,但是看起来很丑陋。

有没有一种简单/好的方法来从中获取模板参数类型TYPED_TEST

Ric*_*ges 6

答案隐藏在文档中:

#include <gtest/gtest.h>

template<typename T>
struct MyTest : public testing::Test
{
    using MyParamType = T;
};

using MyTypes = testing::Types<int, float>;
TYPED_TEST_CASE(MyTest, MyTypes);

TYPED_TEST(MyTest, MyTestName)
{
    // To refer to typedefs in the fixture, add the 'typename TestFixture::'
    // prefix.  The 'typename' is required to satisfy the compiler.

    using MyParamType  = typename TestFixture::MyParamType;
}
Run Code Online (Sandbox Code Playgroud)

https://github.com/google/googletest/blob/master/googletest/docs/advanced.md

  • `TypeParam` 是一回事吗?它似乎被定义为`T` (4认同)
  • 注意,链接已移动:https://github.com/google/googletest/blob/master/docs/advanced.md (2认同)