Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 710
des4maisons Avatar asked Jun 30 '10 18:06

des4maisons


People also ask

What is test fixture in Google test?

If you find yourself writing two or more tests that operate on similar data, you can use a test fixture. This allows you to reuse the same configuration of objects for several different tests. To create a fixture: Derive a class from ::testing::Test .

How do you write a value parameterized test?

To write value-parameterized tests, first you should define a fixture class. It must be derived from both testing::Test and testing::WithParamInterface<T> (the latter is a pure interface), where T is the type of your parameter values.

What is Test_p?

TEST_P() is useful when you want to write tests with a parameter. Instead of writing multiple tests with different values of the parameter, you can write one test using TEST_P() which uses GetParam() and can be instantiated using INSTANTIATE_TEST_SUITE_P() . Example test.


2 Answers

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) { ... } 
like image 121
mabraham Avatar answered Sep 18 '22 15:09

mabraham


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) { ... }
like image 29
VladLosev Avatar answered Sep 19 '22 15:09

VladLosev