我有一些测试:
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)
{
// ...
}
没有不必要的代码重复
最佳答案
除了一个小异常(exception)(两个测试不能有相同的名称),这应该是正确的方向:
#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 的不同调用之间的命名冲突,我将 Name 附加到模板名称和测试方法名称。 TEST_F 宏调用提供了一个名称和一个索引。我没有对其进行测试,因为我没有 Google Test,但其中许多测试框架的机制都是相似的。
关于c++ - 我如何装饰谷歌测试夹具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15088592/