4

Say I have the following class and parameterized test:

class SimpleTest : public ::testing::Test, public ::testing::WithParamInterface<int> {};

INSTANTIATE_TEST_CASE_P(SimpleTests, SimpleTest, ::testing::Range(1, 10));

TEST_P(SimpleTest, TestGreaterThanZero) {
    int i = GetParam();
    ASSERT_GT(i, 0);
}

When I run googletest, I get 10 lines of output, one for each parameter in the range.

Now say I want to change my range from 10 to 10 million. If I ran that, I would get 10 million lines of output (which would be way too much). Is there a way I could group all the output for this parameterized test into one, and simply report the first error?

I know I could rewrite my test case to loop through the values and assert on each one, but I was wondering if there was a way to do this from googletest. The reason why this solution isn't optimal is that if I had multiple parameterized tests, I would have to repeat the loop for each one.

Andrew
  • 1,355
  • 2
  • 13
  • 28

1 Answers1

3

You can change how the output of your tests is printed by deleting the default printer and adding your own. Read this part of google-test advance guide.

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);
  // Gets hold of the event listener list.
  ::testing::TestEventListeners& listeners =
      ::testing::UnitTest::GetInstance()->listeners();
  // delete default printer
  delete listeners.Release(listeners.default_result_printer());
  // add your own
  listeners.Append(new MinimalistPrinter);
  return RUN_ALL_TESTS();  return RUN_ALL_TESTS();
}

How one can define such MinimalistPrinter? It is done by subclassing ::testing::EmptyTestEventListener. Just override OnTestPartResult to collect failures, and override OnTestEnd to print one-line summary.


Or you might just try --gtest_break_on_failure option.

PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
  • Great answer. I was wondering if it was also possible to edit the way the XML file is generated by GoogleTest? – Andrew Feb 03 '17 at 21:11
  • refer to http://stackoverflow.com/questions/8268584/generate-google-c-unit-test-xml-report and/or https://github.com/google/googletest/issues/30 and/or this discussion: https://groups.google.com/forum/#!topic/googletestframework/HVGPe9leCns – PiotrNycz Feb 04 '17 at 18:21