When using a parametrized test in gtest where the parameters are std::pair
, when the test fails, the output is like
[ FAILED ] 1 test, listed below:
[ FAILED ] MyTest/MyFixtureTest.MyTest/1, where GetParam() = (4-byte object <00-00 00-00>, 4-byte object <01-00 00-00>)
How to pretty print the error, for example in case of enums showing the int or the enum value?
The test is something like this
enum class A
{
A=0,
B,
C
};
enum class B
{
A=0,
B,
C
};
B convertAtoB(A a)
{
if (a==A::A)
{
return B::A;
}
else if (a==A::B)
{
return B::B;
}
else
{
return B::C;
}
}
class MyFixtureTest :
public ::testing::TestWithParam<std::pair<A, B>>
{
};
TEST_P(MyFixtureTest, MyTest) {
A a = GetParam().first;
EXPECT_EQ(convertAtoB(a), GetParam().second);
}
INSTANTIATE_TEST_CASE_P(
MyTestP,
MyFixtureTest,
::testing::Values(
std::make_pair(A::A, B::B),
std::make_pair(A::B, B::B),
std::make_pair(A::C, B::C)
)
);
Use the same technique as in Teaching GoogleTest How to Print Your Values. E.g. define PrintTo
:
void PrintTo(const std::pair<A, B>& p, std::ostream* os) {
*os << "(" << (int)p.first << "," << (int)p.second << ")";
}
You can also define operator<<
for your enums to avoid casting to int
.
As a side note, googletest knows how to print std::pair
, it just didn't know how to print your custom types.