使用 google test 将类型名和字符串传递给参数化测试

发布于 2024-09-09 16:26:39 字数 359 浏览 6 评论 0原文

有没有一种方法可以使用谷歌的测试将类型和字符串传递给参数化测试。

我想做:

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 = ..;
  ...
}

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空‖城人不在 2024-09-16 16:26:39

值参数化测试不适用于传递类型信息;您只能通过类型化或类型参数化测试来做到这一点。在这两种情况下,您都必须将类型和字符串信息打包到特殊的结构中。以下是如何使用 来完成此操作类型参数化测试

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);

现在您必须定义参数结构并实例化它们的测试:

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);

您可以使用宏来简化参数类型的定义:

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

然后参数结构的定义变得更短:

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

这相当很复杂,但没有简单的方法可以做到这一点。我最好的建议是尝试重构您的测试,以避免同时需要类型和值信息。但如果你必须这样做,这就是方法。

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.

蒗幽 2024-09-16 16:26:39

我没有研究 TYPED_TEST_P 系列宏在内部的作用,但我发现使用它们来实现目标非常复杂。您只需将参数编码为类型的一部分即可实现相同的目的。

#define TSLP(name, value) \
struct Tslp##name \
{ \
   std::string operator()() \
   { \
      return value; \
   } \
}

TSLP(Empty, "");
TSLP(Foo, "foo");
template<class Type, class Param>
class ParamTextFixture : public Type {
    static std::string message() {
        return Param()();
    }
}

那么您需要做的就是在类型中用 ParamTextFixture 替换您的类

typedef ::testing::Types<
    ParamTextFixture<MyClass, TslpEmpty>
    , ParamTextFixture<MyClass, TslpFoo> >
...

I did not research what TYPED_TEST_P family of macros does internally, but I find using them extra complicated for the goal. You could achieve the same simply encoding the parameter as part of the type.

#define TSLP(name, value) \
struct Tslp##name \
{ \
   std::string operator()() \
   { \
      return value; \
   } \
}

TSLP(Empty, "");
TSLP(Foo, "foo");
template<class Type, class Param>
class ParamTextFixture : public Type {
    static std::string message() {
        return Param()();
    }
}

then all you need to do is in the types to replace your class with the ParamTextFixture

typedef ::testing::Types<
    ParamTextFixture<MyClass, TslpEmpty>
    , ParamTextFixture<MyClass, TslpFoo> >
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文