Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a typename and string to parameterized test using google test

Tags:

c++

googletest

Is there a way of passing both a type and a string to a parametrized test using google's test.

I would like to do:

template <typename T>
class RawTypesTest : public ::testing::TestWithParam<const char * type> {
protected:
  virtual void SetUp() {
       message = type;
  }
}; 

TEST_P(RawTypesTest, Foo) {
  ASSERT_STREQ(message, type);
  ParamType * data = ..;
  ...
}

Thanks in advance

like image 379
adk Avatar asked Jul 15 '10 17:07

adk


People also ask

How do you write a value parameterized test?

How to Write Value-Parameterized Tests. To write value-parameterized tests, first you should define a fixture class. It must be derived from both testing::Test and testing::WithParamInterface<T> (the latter is a pure interface), where T is the type of your parameter values.

How do I run a Gtest disabled test?

If you know a test will fail before running it, then you can disable it temporarily by prepending DISABLED_ to the test name.


1 Answers

Value parameterized tests won't work for passing type information; you can only do that with typed or type parameterized tests. In both cases you'll have to package your type and string information into special structures. Here is how it can be done with type-parameterized tests:

template <typename T> class RawTypesTest : public testing::Test {
 public:
  virtual void SetUp() {
    this->message_ = TypeParam::kStringValue;
  }

 protected:
  const char* const message_;
};

TYPED_TEST_CASE_P(RawTypesTest);

TYPED_TEST_P(RawTypesTest, DoesFoo) {
  ASSERT_STREQ(message, TypeParam::kStringValue);
  TypeParam::Type* data = ...;
}

TYPED_TEST_P(RawTypesTest, DoesBar) { ... }

REGISTER_TYPED_TEST_CASE_P(FooTest, DoesFoo, DoesBar);

And now you have to define the parameter structures and instantiate the tests for them:

struct TypeAndString1 {
  typedef Type1 Type;
  static const char* kStringValue = "my string 1";
};
const char* TypeAndString1::kStringValue;

struct TypeAndString2 {
  typedef Type1 Type;
  static const char* kStringValue = "my string 2";
};
const char* TypeAndString2::kStringValue;

typedef testing::Types<TypeAndString1, TypeAndString2> MyTypes;
INSTANTIATE_TYPED_TEST_CASE_P(OneAndTwo, RawTypeTest, MyTypes);

You can use a macro to simplify definition of your parameter types:

#define MY_PARAM_TYPE(name, type, string) \
  struct name { \
    typedef type Type; \
    static const char kStringValue = string; \
  }; \
  const char* name::kStringValue

Then definitions of parameter structs become much shorter:

MY_PARAM_TYPE(TypeAndString1, Type1, "my string 1");
MY_PARAM_TYPE(TypeAndString2, Type2, "my string 2");

This is quite complicated but there is no easy way to do this. My best advice is to try re-factor your tests to avoid requiring both type and value information. But if you must, here is the way.

like image 197
VladLosev Avatar answered Sep 20 '22 19:09

VladLosev