使用 google test 将类型名和字符串传递给参数化测试
有没有一种方法可以使用谷歌的测试将类型和字符串传递给参数化测试。
我想做:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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:
And now you have to define the parameter structures and instantiate the tests for them:
You can use a macro to simplify definition of your parameter types:
Then definitions of parameter structs become much shorter:
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.
我没有研究 TYPED_TEST_P 系列宏在内部的作用,但我发现使用它们来实现目标非常复杂。您只需将参数编码为类型的一部分即可实现相同的目的。
那么您需要做的就是在类型中用 ParamTextFixture 替换您的类
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.
then all you need to do is in the types to replace your class with the ParamTextFixture