C++ Google Test:使用现有测试夹具类的参数化测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3152326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:12:14  来源:igfitidea点击:

Google Test: Parameterized tests which use an existing test fixture class?

c++unit-testinggoogletest

提问by des4maisons

I have a test fixture class which is currently used by many tests.

我有一个当前被许多测试使用的测试夹具类。

#include <gtest/gtest.h>
class MyFixtureTest : public ::testing::Test {
  void SetUp() { ... }
};

I would like to create a parameterized test which also uses all that MyFixtureTest has to offer, without needing to change all my existing tests.

我想创建一个参数化测试,它也使用 MyFixtureTest 提供的所有功能,而无需更改我所有现有的测试。

How do I do that?

我怎么做?

I have found similar discussions on the web, but have not fully understood their answers.

我在网上找到了类似的讨论,但还没有完全理解他们的答案。

回答by mabraham

This question is now answered in the Google Test documentation(the answerfrom VladLosev is technically correct, but perhaps slightly more work)

这个问题现在在谷歌测试文档中得到回答(来自 VladLosev的回答在技​​术上是正确的,但可能需要更多的工作)

Specifically, when you want to add parameters to a pre-existing fixture class, you can do

具体来说,当您想将参数添加到预先存在的夹具类时,您可以这样做

class MyFixtureTest : public ::testing::Test {
  ...
};
class MyParamFixtureTest : public MyFixtureTest,
                           public ::testing::WithParamInterface<MyParameterType> {
  ...
};

TEST_P(MyParamFixtureTest, MyTestName) { ... }

回答by VladLosev

The problem is that for regular tests your fixture has to be derived from testing::Test and for parameterized tests, it has to be derived from testing::TestWithParam<>.

问题是,对于常规测试,您的装置必须从 testing::Test 派生,对于参数化测试,它必须从 testing::TestWithParam<> 派生。

In order to accommodate that, you'll have to modify your fixture class in order to work with your parameter type

为了适应这种情况,您必须修改夹具类才能使用您的参数类型

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> > {};

This way you can keep all your existing tests intact while creating parameterized tests using

通过这种方式,您可以在使用以下方法创建参数化测试时保持所有现有测试不变

TEST_P(MyParamFixtureTest, MyTestName) { ... }

回答by ratkok

If you create a new fixture that is derived from this common one and than create your parameterized tests on that derived class - would that help you and solve your problem?

如果您创建一个从这个通用夹具派生的新夹具,然后在该派生类上创建参数化测试 - 这会帮助您解决您的问题吗?

From Google Test wiki page: "In Google Test, you share a fixture among test cases by putting the shared logic in a base test fixture, then deriving from that base a separate fixture for each test case that wants to use this common logic."

来自 Google Test wiki 页面:“在 Google Test 中,您可以通过将共享逻辑放在基础测试夹具中来在测试用例之间共享夹具,然后从该基础为每个想要使用此公共逻辑的测试用例派生一个单独的夹具。”