I have some functions on my gtest, like below. (don't mind the syntax)
TEST(HotelTest, test1)
{
MyObj obj;
int a = obj.doStuff(1);
EXPECT_EQ(test1, a);
}
TEST(HotelTest, test2)
{
MyObj obj;
int a = obj.doStuff(2);
EXPECT_EQ(test2, a);
}
TEST(HotelTest, test3)
{
MyObj obj;
int a = obj.doStuff(3);
EXPECT_EQ(test3, a);
}
TEST(HotelTest, test4)
{
MyObj obj;
int a = obj.doStuff(4);
EXPECT_EQ(test4, a);
}
As you can see, there is code repeated all over the functions.
QUESTION: Is there any way to avoid this? I mean... Something like an inline function where I could store that repeated code in another place and then just call?
P.S. I really need to have different TEST
functions. This example is just to show the problem.
Thanks in advance:)
At the very least, you could use test fixtures.
This will allow you to avoid duplicating the
MyObj obj;
line in each case:If your calls to
obj.doStuff
are more involved than a single command, you can wrap these in a function of the fixture too:Finally, you might be able to make use of the more advanced value-parameterised tests. This example would run the test case four times. On the first iteration
GetParam()
would yield1
, on the second2
and so on up to4
as specified by::testing::Range(1, 5)
.