I am trying to write a test with parameterization of values, where test values are created only after test classes have been created, i.e. test values are stored in a non-static variable. This means that I cannot do what I usually do when the container is static:
INSTANTIATE_TEST_CASE_P(SomeCriteria, SomeTest,
ValuesIn(SomeClass::staticContainerWithTestINputs) );
Here is an example of MVCE when I was stuck:
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using namespace testing;
struct CustomClass
{
int myInt = 0;
};
class Fixture : public ::testing::Test {
protected:
CustomClass myCustomCls;
virtual void SetUp() override
{
myCustomCls.myInt = 42;
}
};
class ValueParamTest : public Fixture, public WithParamInterface<int> {
public:
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);
}
INSTANTIATE_TEST_CASE_P(ValidInputValues, ValueParamTest,
ValuesIn(ValueParamTest::validInputs) );
Compiler Error:
59: error: invalid use of non-static data member ‘ValueParamTest::validInputs’
ValuesIn(ValueParamTest::validInputs) );
^
There is no instance of this class ValueParamTest, so I cannot access its instance data members or member functions.
Can anyone give a hint how this can be done in GTest?
source
share