当在gtest中使用参数化测试时,参数为
std::pair
,当测试失败时,输出如下
[ FAILED ] 1 test, listed below:
[ FAILED ] MyTest/MyFixtureTest.MyTest/1, where GetParam() = (4-byte object <03-00 00-00>, 4-byte object <02-00 00-00>)
如何漂亮地打印错误,例如在枚举显示 int 或枚举值的情况下?
测试是这样的
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::WithParamInterface<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::A),
std::make_pair(A::B, B::B),
std::make_pair(A::C, B::C)
)
);
使用与教 GoogleTest 如何打印您的值中相同的技术。例如。定义
PrintTo
:
void PrintTo(const std::pair<A, B>& p, std::ostream* os) {
*os << "(" << (int)p.first << "," << (int)p.second << ")";
}
您还可以为枚举定义
operator<<
以避免强制转换为 int
。
顺便说一句,googletest知道如何打印
std::pair
,它只是不知道如何打印您的自定义类型。