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.
How to Write Value-Parameterized Tests. To write value-parameterized tests, first you should define a fixture class. It must be derived from both testing::Test and testing::WithParamInterface<T> (the latter is a pure interface), where T is the type of your parameter values.
The EXPECT_EQ() macro takes the expected value as its first argument and the actual value as the second.
TEST_F(TestFixtureName, TestName) { ... statements ... } Defines an individual test named TestName that uses the test fixture class TestFixtureName . The test suite name is TestFixtureName .
gtest-parallel is a script that executes Google Test binaries in parallel, providing good speedup for single-threaded tests (on multi-core machines) and tests that do not run at 100% CPU (on single- or multi-core machines).
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.
The header in the gtest-printers.h
source file provides an answer:
This file implements a universal value printer that can print a value of any type T:
void ::testing::internal::UniversalPrinter::Print(value, ostream_ptr);
A user can teach this function how to print a class type T by defining either operator<<() or PrintTo() in the namespace that defines T. More specifically, the FIRST defined function in the following list will be used (assuming T is defined in namespace foo):
- foo::PrintTo(const T&, ostream*)
- operator<<(ostream&, const T&) defined in either foo or the global namespace.
If none of the above is defined, it will print the debug string of the value if it is a protocol buffer, or print the raw bytes in the value otherwise.
So it looks like the operator override needs to be a non-member function.
std::ostream& operator<<(std::ostream& stream, Line const& line)
{
return stream << "Line (radius=" << line.radius() << " theta=" << line.theta() << ")";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With