Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get gtest TYPED_TEST parameter type

Tags:

c++

googletest

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.

Example:

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

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

By changing to:

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

The code compiles, but it looks very ugly.

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

like image 591
Mircea Ispas Avatar asked Jul 21 '17 09:07

Mircea Ispas


People also ask

How do you pass a Gtest argument?

Use your favorite command-line-parsing technique, store the results in some global variable, and refer to it during your tests. If a command-line options looks like a Google Test option but really isn't, then the program will print its help message and exit without running any tests.

What is Typed_test?

TYPED_TEST(TestSuiteName, TestName) { ... statements ... } Defines an individual typed test named TestName in the typed test suite TestSuiteName . The test suite must be defined with TYPED_TEST_SUITE .

What is Test_f in Gtest?

TEST_F() is useful when you need access to objects and subroutines in the unit test. Example test. TEST_P() is useful when you want to write tests with a parameter.

What is Gtest H?

h. The directory that contains gtest is not in your list of include search paths. gtest/gtest. h is working only because it's relative to the current file. You need to add the path that contains the directory gtest to your list of include search paths.


Video Answer


1 Answers

The answer is hidden in the docs:

#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

like image 62
Richard Hodges Avatar answered Oct 18 '22 07:10

Richard Hodges