![](/img/trans.png)
[英]How to run Parameterized Tests with fixture member values in Google Test (gtest)?
[英]Google Test: Parameterized tests which use an existing test fixture class?
我有一个测试夹具类,目前许多测试都在使用它。
#include <gtest/gtest.h>
class MyFixtureTest : public ::testing::Test {
void SetUp() { ... }
};
我想创建一个参数化测试,该测试还使用MyFixtureTest必须提供的所有功能,而无需更改所有现有测试。
我怎么做?
我在网上发现了类似的讨论,但是还没有完全理解他们的答案。
现在, Google测试文档中已回答了这个问题(VladLosev的回答在技术上是正确的,但可能还有更多工作要做)
具体来说,当您想向现有灯具类添加参数时,可以执行
class MyFixtureTest : public ::testing::Test {
...
};
class MyParamFixtureTest : public MyFixtureTest,
public ::testing::WithParamInterface<MyParameterType> {
...
};
TEST_P(MyParamFixtureTest, MyTestName) { ... }
问题在于,对于常规测试,夹具必须从testing :: Test派生,对于参数化测试,它必须从testing :: TestWithParam <>派生。
为了适应这种情况,您必须修改灯具类才能使用参数类型
template <class T> class MyFixtureBase : public T {
void SetUp() { ... };
// Put the rest of your original MyFixtureTest here.
};
// This will work with your non-parameterized tests.
class MyFixtureTest : public MyFixtureBase<testing::Test> {};
// This will be the fixture for all your parameterized tests.
// Just substitute the actual type of your parameters for MyParameterType.
class MyParamFixtureTest : public MyFixtureBase<
testing::TestWithParam<MyParameterType> > {};
这样,您可以在使用以下命令创建参数化测试时保持所有现有测试不变
TEST_P(MyParamFixtureTest, MyTestName) { ... }
如果您创建了一个从该通用夹具派生的新夹具,然后在该派生类上创建了参数化测试,那么这将对您有所帮助并解决您的问题吗?
在Google Test Wiki页面上 :“在Google Test中,您可以通过将共享逻辑放在基本测试夹具中来在测试用例之间共享夹具,然后从该底座中为想要使用此通用逻辑的每个测试用例派生一个单独的夹具。”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.