我如何装饰谷歌测试夹具
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作为从两个fixture之一继承的fixture。由于do_test是MyTester的成员,它可以访问从fixture继承的所有成员。测试框架将为每个测试创建一个MyTester对象,并将相应的实际fixture创建为基类对象。为了避免与其他测试或不同TEST_F2调用之间的命名冲突,我将Name附加到模板名和测试方法名之后。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的公共fixture。您可以在测试中访问这些公共对象。
您可以采用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有两种方法在不同的上下文中执行相同的测试体:值参数化测试或类型/类型参数化测试。不完全是你想要的,但它是最接近它提供的。
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 尽管测试成功,CppUnit测试核心仍被丢弃.为什么
- 数据成员SFINAE的C++17测试:gcc vs clang
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么二进制搜索在我的测试中不起作用
- 谷歌测试:如何为多个测试只运行一次夹具?
- 从谷歌测试中的派生夹具派生夹具
- GMock 测试夹具在 Windows 上崩溃
- 如何在Fitnesse测试中生成夹具的文档
- Boost单元测试夹具继承测试类,是否可以访问受保护的方法
- C++谷歌测试在哪里放置测试夹具构造函数定义
- 使用夹具时的升压单元测试过滤器
- 测试夹具中的设置与构造函数
- 不可实例化的谷歌测试夹具超级类
- 谷歌测试框架参数化夹具
- GoogleTest C++-测试夹具
- 我如何装饰谷歌测试夹具
- 无法引用测试夹具的默认构造函数
- 如何在谷歌测试中为一个夹具运行多个测试用例