Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

googletest

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.

like image 920
Drew Noakes Avatar asked Mar 03 '13 21:03

Drew Noakes


People also ask

How do you write a value parameterized test?

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.

What is Expect_eq?

The EXPECT_EQ() macro takes the expected value as its first argument and the actual value as the second.

What is Test_f in Gtest?

TEST_F(TestFixtureName, TestName) { ... statements ... } Defines an individual test named TestName that uses the test fixture class TestFixtureName . The test suite name is TestFixtureName .

Does Gtest run tests in parallel?

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).


2 Answers

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.

like image 81
Vasily Fomin Avatar answered Oct 14 '22 23:10

Vasily Fomin


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):

  1. foo::PrintTo(const T&, ostream*)
  2. 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() << ")";
}
like image 26
Drew Noakes Avatar answered Oct 14 '22 23:10

Drew Noakes