Search code examples
c++googletest

Customise actual/expected "Value of" string in Google Test failure output messages


I have the following output from a Google Test unit test:

UnitTests.cc:56: Failure
Value of: LineSegment2i(Vector2i(-10,0), Vector2i(-10,10)).toLine()
  Actual: 24-byte object <00-00 00-00 00-00 24-C0 00-00 00-00 00-00 00-00 00-00 2F-2B FF-7F 00-00>
Expected: Line(10, 3.14159265358979323846)
Which is: 24-byte object <00-00 00-00 00-00 24-40 18-2D 44-54 FB-21 09-40 00-00 64-00 00-00 00-00>
[  FAILED  ] LineSegmentTests.toLine (1 ms)

That hexadecimal output string isn't very useful. Is there something I can add to the Line class (for which an equality test is failing) to provide more helpful errors in such cases?

The class in question has overridden the << operator as a member function:

std::ostream& operator<<(std::ostream& stream) const
{
  return stream << "Line (radius=" << d_radius << " theta=" << d_theta << ")";
}

You can see that this works for the 'Expected' line, but not the 'Actual' line. This statement is untrue — the test shown comes from the parameter of the TEST macro.


Solution

  • In order to print custom types you could "teach" Google Test how to print your custom types which as described in the section Teaching Google Test How to Print Your Values.