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

des4maisons picture des4maisons · Jun 30, 2010 · Viewed 35.8k times · Source

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.

How do I do that?

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

Answer

mabraham picture mabraham · Jan 14, 2014

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

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