24

現在多くのテストで使用されているテスト フィクスチャ クラスがあります。

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

既存のすべてのテストを変更する必要なく、MyFixtureTest が提供するすべてを使用するパラメーター化されたテストを作成したいと考えています。

それ、どうやったら出来るの?

Web で同様の議論を見つけましたが、その回答を完全には理解していません。

4

3 に答える 3

24

問題は、通常のテストの場合、フィクスチャを 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) { ... }
于 2010-07-14T21:55:54.017 に答える
0

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

于 2010-07-02T04:51:51.123 に答える