DBedrenko
DBedrenko

Reputation: 5029

How to write value-parameterized tests for values in a non-static container?

I'm trying to write a value-parameterized test, where the test values are only created once the test classes have been instantiated, i.e. the test values are stored in a non-static variable. This means I cannot do what I normally do, where the container is static:

INSTANTIATE_TEST_CASE_P(SomeCriteria, SomeTest,
                    ValuesIn(SomeClass::staticContainerWithTestINputs) );

Here is an MVCE example at the point I am stuck:

#include <gmock/gmock.h>
#include <gtest/gtest.h>
using namespace testing;

// This is not a test class, so I can't modify `myInt` to be static just so
// that I can write tests.
struct CustomClass
{
  int myInt = 0;
};

class Fixture : public ::testing::Test {
protected:
  CustomClass myCustomCls;

  virtual void SetUp() override
  {
    // This variable needs to be used in the parameterized test.
    myCustomCls.myInt = 42;
  }
};

class ValueParamTest : public Fixture, public WithParamInterface<int> {
public:
  // The container holding the values to be tested.
  const std::vector<int> validInputs {
    1, 24, myCustomCls.myInt
  };

protected:
  virtual void SetUp()
  {
    Fixture::Fixture::SetUp();
    mTestInput = GetParam();
  }

  int mTestInput;
};


TEST_P(ValueParamTest, ValidInputs)
{
  EXPECT_TRUE(mTestInput < 100);
}

// COMPILER ERROR HERE
INSTANTIATE_TEST_CASE_P(ValidInputValues, ValueParamTest,
                        ValuesIn(ValueParamTest::validInputs) );

The compiler error:

59: error: invalid use of non-static data member ‘ValueParamTest::validInputs’
                         ValuesIn(ValueParamTest::validInputs) );
                                                  ^

There is no instance of that ValueParamTest class, so I cannot access its instance data members or member functions.

Anyone could give a hint how this could be done in GTest?

Upvotes: 8

Views: 4151

Answers (2)

Mike Kinghan
Mike Kinghan

Reputation: 61432

Seemingly Googletest's macro repertoire does not run to your requirement, but by The Fundamental Theorem of Software Engineering, you can do the like of this:-

main.cpp

#include <gtest/gtest.h>
#include <functional>
#include <memory>
using namespace testing;

struct CustomClass
{
    int myInt = 0;
};

class Fixture : public ::testing::Test {
protected:
    static std::shared_ptr<CustomClass> & getSpecimen() {
        static std::shared_ptr<CustomClass> specimen;
        if (!specimen) {
            specimen.reset(new CustomClass{42});
        }
        return specimen;
    }
    void TearDown() override
    {
        getSpecimen().reset();
    }
};

class ValueParamTest : 
    public Fixture, public WithParamInterface<std::function<int()>> {
public:
    static std::vector<std::function<int()>> validInputs;

protected:
    void SetUp() override {
        mTestInput = GetParam()();
    }
    void TearDown() override {
        Fixture::TearDown();
    }

    int mTestInput;
};

std::vector<std::function<int()>> ValueParamTest::validInputs{
    []() { return 1; },
    []() { return 24; },
    []() { return ValueParamTest::getSpecimen()->myInt; }
}; 


TEST_P(ValueParamTest, ValidInputs)
{
    std::cout << "mTestInput = " << mTestInput << std::endl;
    EXPECT_TRUE(mTestInput < 100);
}

INSTANTIATE_TEST_CASE_P(ValidInputValues, ValueParamTest,
                        ValuesIn(ValueParamTest::validInputs) );

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

Which builds and runs like:

g++ -Wall -std=c++14 -o gtestrun main.cpp -lgtest -pthread && ./gtestrun 
[==========] Running 3 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 3 tests from ValidInputValues/ValueParamTest
[ RUN      ] ValidInputValues/ValueParamTest.ValidInputs/0
mTestInput = 1
[       OK ] ValidInputValues/ValueParamTest.ValidInputs/0 (0 ms)
[ RUN      ] ValidInputValues/ValueParamTest.ValidInputs/1
mTestInput = 24
[       OK ] ValidInputValues/ValueParamTest.ValidInputs/1 (1 ms)
[ RUN      ] ValidInputValues/ValueParamTest.ValidInputs/2
mTestInput = 42
[       OK ] ValidInputValues/ValueParamTest.ValidInputs/2 (0 ms)
[----------] 3 tests from ValidInputValues/ValueParamTest (1 ms total)

[----------] Global test environment tear-down
[==========] 3 tests from 1 test case ran. (1 ms total)
[  PASSED  ] 3 tests.

Upvotes: 4

pptaszni
pptaszni

Reputation: 8228

I think that it might not be possible to instantiate the TEST_P with dynamically generated values, because INSTANTIATE_TEST_CASE_P is a macro that actually defines new functions for each parameter value. The only solution/workaround to your problem that comes to my mind is simply to check all the inputs to your SUT in the for loop in normal test:

using namespace testing;

// This is not a test class, so I can't modify `myInt` to be static just so
// that I can write tests.
struct CustomClass
{
  int myInt = 0;
};

class Fixture : public ::testing::Test {
protected:
  CustomClass myCustomCls;

  virtual void SetUp() override
  {
    // This variable needs to be used in the parameterized test.
    myCustomCls.myInt = 42;
  }
};

class ValueParamTest : public Fixture {
public:
  // The container holding the values to be tested.
  const std::vector<int> validInputs {
    1, 24, myCustomCls.myInt, 101, 99, 102
  };

protected:
  virtual void SetUp()
  {
    Fixture::Fixture::SetUp();
  }
};


TEST_F(ValueParamTest, ValidInputs)
{
    std::for_each(validInputs.begin(), validInputs.end(),
        [](int v){ EXPECT_TRUE(v < 100) << "invalid input: " << v; });
}

Of course then it would be treated as just one test case with all its disadvantages (and advantages).

If I am wrong, let me know please. It would be interesting to generate parametrized test cases dynamically.

Upvotes: 1

Related Questions