'Use arbitrary classes as parameter for parametrized tests in googletest
I have some repetive tests where the input of the tests needs to be created uniquely in a non-repetive way. Imagine 2 tests like this:
struct ComplexInput{};
ComplexInput CreateA();
ComplexInput CreateB();
bool FunctionUnderTest(ComplexInput& input);
TEST(TestCase, TestWithA)
{
auto input = CreateA();
auto ret = FunctionUnderTest(input);
EXPECT_TRUE(ret);
}
TEST(TestCase, TestWithB)
{
auto input = CreateB();
auto ret = FunctionUnderTest(input);
EXPECT_TRUE(ret);
}
Is there any way to parametrize the tests to specify the function which should be called e.g. with templates? The only solution I can think of is using an enum to specify the data and using a function which returns the correct data based on the enum type, but this feels like a hack:
struct ComplexInput{};
ComplexInput CreateA();
ComplexInput CreateB();
bool FunctionUnderTest(ComplexInput& input);
class enum InputChoice
{
A,
B
};
ComplexInput GetComplexInput(InputChoice c)
{
switch(c)
{
case A:
return CreateA();
case B:
return CreateB();
}
}
class ParamTest : public ::testing::TestWithParam<InputChoice>{};
TEST_P(ParamTest, Test)
{
InputChoice c = GetParam();
auto input = GetComplexInput(c);
auto ret = FunctionUnderTest(input);
EXPECT_TRUE(ret);
}
INSTANTIATE_TEST_CASE_P
(
Tests,
ParamTest,
::testing::Values
(
InputChoice::A,
InputChoice::B
// maybe this could also be automated?
)
);
Please keep in mind that this code is only to demonstrate the scenario and it could be that it does not compile.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
