本文介绍了Google测试:使用现有测试夹具类的参数化测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试夹具类,目前许多测试都在使用它.

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

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

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

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.

我该怎么做?

我在网络上发现了类似的讨论,但是还没有完全理解他们的答案.

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

推荐答案

问题在于,对于常规测试,您的灯具必须从testing :: Test派生,对于参数化测试,它的灯具必须从testing :: TestWithParam< ;>.

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<>.

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

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) { ... }

这篇关于Google测试:使用现有测试夹具类的参数化测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 12:50