如何获取gtest TYPED_TEST参数类型 [英] How to get gtest TYPED_TEST parameter type

查看:200
本文介绍了如何获取gtest TYPED_TEST参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some unit tests written on Windows (Visual Studio 2017) and I need to port them on Linux (GCC 4.9.2 - I'm stuck with this version...). I've come with a simple example for my problem which compiles fine on Windows (I don't think it should compile as MyParamType is a dependent type from e template base class) and doesn't compile on 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;
} 

在成员函数虚拟无效"中 MyTest_MyTestName_Test :: TestBody()":错误:未在此范围内声明"MyParamType" MyParamType参数;

In member function ‘virtual void MyTest_MyTestName_Test::TestBody()’:error: ‘MyParamType’ was not declared in this scope MyParamType param;

更改为:

TYPED_TEST(MyTest, MyTestName)
{
    typename MyTest<gtest_TypeParam_>::MyParamType param;
}

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

The code compiles, but it looks very ugly.

是否有一种简便/好方法从TYPED_TEST获取模板参数类型?

Is there an easy/nice way to get the template parameter type from a TYPED_TEST?

推荐答案

答案隐藏在文档中:

#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;
}

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

这篇关于如何获取gtest TYPED_TEST参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆