Googletest: Repeating the same test code for multiple fixtures?

Created on 27 Jun 2017  路  9Comments  路  Source: google/googletest

Imagine I have a couple of fixtures F1, F2, and I want to run the same test code on them. And that I want to test multiple implementations of the same interface.

TEST(F1, name) { ... test code ... }
TEST(F2, name) { ... same test code again ... }

Is there a nice way to do this without

  • using value parameterized tests (passing parameters that make a single fixture behave like F1 or F2
  • templating the tests and passing F1 and F2 as template parameter?
  • putting the test code into a function

I imagine something like

TEST_DEFINE_WITHOUT_FIXTURE(name) { ... test code ... }
INSTANTIATE_TEST(F1, name)
INSTANTIATE_TEST(F2, name)

Thanks for this great tool!

Most helpful comment

It is a pitty this wasn't addressed since it would be a very nice feature to have

All 9 comments

@lisitsyn @vigsterkr

In case you find some different solution, you may add it to this question in the FAQ?

I worked around it for now, but this leads to duplicate code, un-intuitive structure etc. Would be a nice feature

@karlnapf do you have a reference to your technique?

I define an enum with the different test contexts, e.g.

enum class ETestType
{
    E_CASE_A,
    E_CASE_B,
};

My fixture class uses the GetParam() to switch setting up the test context in the SetUp method:

class MyFixture: public ::testing::TestWithParam<ETestType>
{
public:
    void SetUp()
    {
        auto test_type = GetParam();
        switch (test_type)
        {
            case ETestType::E_CASE_A:
            {
                // set up test environment a
                break;
            }
            case ETestType::E_CASE_B:
            {
                // set up test environment b
                break;
            }
            ...
        }
    }
};

Then I define my parametrised tests:

TEST_P(MyFixture, test_for_multiple_contexts)
{
    // test code that I want to execute for all different contexts
}

And then finally instantiating the tests

INSTANTIATE_TEST_CASE_P(Bla, MyFixture, ::testing::Values(ETestType::E_CASE_A, ETestType::E_CASE_B)
);

You see, this is rather laborious. I would prefer to use googletest's macro magic.

@karlnapf Thanks. This is very similar to what I came up with as well. I can easily see how having more than a few cases could become quite fragile.

I have been cleaning up older and inactive GitHub googletest issues. You may see an issue "closed" if it appears to be inactive/abandoned
Thank you

It is a pitty this wasn't addressed since it would be a very nice feature to have

I don't understand the reason why this is closed. Is it because somebody actively decided this will not be pursued, or just because nobody commented? Would be nice to know if the reason is a technical one, otherwise it should go to a wishlist.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

markfrazzetto picture markfrazzetto  路  3Comments

s-perron picture s-perron  路  6Comments

pepe82sh picture pepe82sh  路  5Comments

tschijnmo picture tschijnmo  路  4Comments

GoogleCodeExporter picture GoogleCodeExporter  路  5Comments