[英]How can i decorate Google Test fixture
我有一些测试:
class Somefixture: ::testing::Test{};
class Somefixture2: ::testing::Test{};
TEST_F(SomeFixture, SomeName)
{
// ...
}
如何自动将测试链接到两个灯具(装饰)?
TEST_F2(SomeFixture, SomeFixture2, SomeName){}
虽然所需的结果就像我写的那样:
TEST_F(SomeFixture, SomeName)
{
// ...
}
TEST_F(SomeFixture2, SomeName)
{
// ...
}
没有不必要的代码重复
除了一个小例外(两个测试不能使用相同的名称),这应该正确地执行:
#define TEST_F2(F1, F2, Name) \
template <struct Fixture> struct MyTester##Name : Fixture { \
void test##Name(); \
}; \
\
TEST_F(MyTester##Name<F1>, Name##1){ test##Name(); } \
TEST_F(MyTester##Name<F2>, Name##2){ test##Name(); } \
\
template <struct Fixture> void MyTester##Name::test##Name()
这将调用两个测试,每个测试都使用MyTester作为从两个固定装置之一继承的固定装置。 由于do_test是MyTester的成员,因此它可以访问固定装置中所有继承的成员。 测试框架将为每个测试创建一个MyTester对象,并且相应的实际灯具将被创建为基类对象。 为避免与其他测试的命名冲突或在TEST_F2的不同调用之间发生命名冲突,我在模板名称和测试方法名称后附加了名称。 TEST_F宏调用提供了名称和索引。 我没有进行测试,因为我没有Google Test,但是许多测试框架中的机制都类似。
如何自动将测试链接到两个灯具(装饰)?
通过添加一个通用的基类:
class CommonFixture
{
public:
// add member variables and initialize them in the constructor
};
class Somefixture1 : ::testing::Test, protected CommonFixture{}
class Somefixture2 : ::testing::Test, protected CommonFixture{}
测试保持不变:
TEST_F(SomeFixture1, SomeName)
{
// ...
}
TEST_F(SomeFixture2, SomeName)
{
// ...
}
现在,您已经为Somefixture1和Somefixture2有了一个通用的灯具。 您可以从测试中访问这些通用对象。
您可以采用似乎不错的BЈовић
方法。
或者需要对测试本身进行少量更改的另一种方法可以是具有“超级”类,该类将两个实例都作为成员。
class superFuxture
{
public:
Somefixture1 f1;
Somefixture2 f2;
}
然后,您将进行如下测试:
TEST_F(superFuxture, SomeName)
{
//when you were accessing a member of Somefixture1 you'll now need to do:
//f1.SomeFixture1Member
}
Google Test有两种在不同上下文中执行同一测试主体的方式: 值参数化测试或类型/类型参数化测试 。 不完全是您想要的,而是它提供的最接近的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.