Printing additional output in Google Test

C++Googletest

C++ Problem Overview


I'm using the googletest C++ testing framework. Normally the textual output of running a test looks like this:

[ RUN      ] MyTest.Fuzz
[       OK ] MyTest.Fuzz (1867 ms)

I would like to output some additional data in the same format, for example:

[ RUN      ] MyTest.Fuzz
[          ] random seed = 1319760587
[       OK ] MyTest.Fuzz (1867 ms)

I have found Logging Additional Information in the googletest documentation but that only seems to send structured data to the XML output, not the standard console output.

Is there a googletest function I can call inside my unit test that outputs text in this format? Manually sending data to cout works, but it doesn't include the usual coloured output so I have to explicitly indent the output by printing 13 spaces or whatever.

C++ Solutions


Solution 1 - C++

Simply printing to stderr will work in the default test configuration.

std::cerr << "[          ] random seed = " << random_seed << std::endl;

Solution 2 - C++

You could write a wrapper for the default printer PrettyUnitTestResultPrinter to also print out test properties. You can get the default printer with listeners.default_result_printer()(see below). You would have to implement EmptyTestEventListener and change the method PrettyUnitTestResultPrinter::OnTestEnd() (in gtest.cc) and use the results properties: test_info.result()->GetTestProperty(i) like the printer for XML output:

String XmlUnitTestResultPrinter::TestPropertiesAsXmlAttributes(
    const TestResult& result) {
  Message attributes;
  for (int i = 0; i < result.test_property_count(); ++i) {
    const TestProperty& property = result.GetTestProperty(i);
    attributes << " " << property.key() << "="
        << "\"" << EscapeXmlAttribute(property.value()) << "\"";
  }
  return attributes.GetString();
}

Then you can replace the default listener for your tests like it's done in the google test sample:

UnitTest& unit_test = *UnitTest::GetInstance();
if (terse_output) {
  TestEventListeners& listeners = unit_test.listeners();
  delete listeners.Release(listeners.default_result_printer());
  listeners.Append(new TersePrinter);
}
int ret_val = RUN_ALL_TESTS();

Solution 3 - C++

I have just used std::cout with ansi color codes in linux but I believe the codes work in windows since win 10 anniversary update.

std::cout << "\033[0;32m" << "[          ] " << "\033[0;0m" 
<< "random seed = " << random_seed << std::endl;

or just create a header file and some #define statements and include it in my tests. I also like to format the text to stick out a little more too.

#define ANSI_TXT_GRN "\033[0;32m"
#define ANSI_TXT_MGT "\033[0;35m" //Magenta
#define ANSI_TXT_DFT "\033[0;0m" //Console default
#define GTEST_BOX "[     cout ] "
#define COUT_GTEST ANSI_TXT_GRN << GTEST_BOX //You could add the Default
#define COUT_GTEST_MGT COUT_GTEST << ANSI_TXT_MGT

So my code would be:

std::cout << COUT_GTEST_MGT << "random seed = " << random_seed << ANSI_TXT_DFT << std::endl;

Solution 4 - C++

Nope, searched through the headers and there is nothing about adding your own logs in the middle. Might be something to request. You could log an enhancement task if you want at http://code.google.com/p/googletest/issues/list

Take care.

Solution 5 - C++

following works, if you save in a .cmd file and then run it

https://gist.githubusercontent.com/mlocati/fdabcaeb8071d5c75a2d51712db24011/raw/b710612d6320df7e146508094e84b92b34c77d48/win10colors.cmd

example of codes:

@echo off
cls
echo  STYLES 
echo ^<ESC^>[0m Reset
echo ^<ESC^>[1m Bold
echo ^<ESC^>[4m Underline
echo ^<ESC^>[7m Inverse
echo.

enter image description here

Solution 6 - C++

Printing additional output in gTest also possible like this:

  EXPECT_NE(result1, result2)
      << "currentTime: " << formattedTime << std::endl
      << "  addinfo1: " << addinfo1 << std::endl
      << "  addinfo2: " << addinfo2 << std::endl;

std::cout, std::cerr will not work within gTest.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGreg HewgillView Question on Stackoverflow
Solution 1 - C++Martin NowakView Answer on Stackoverflow
Solution 2 - C++kossmoboleatView Answer on Stackoverflow
Solution 3 - C++LeeView Answer on Stackoverflow
Solution 4 - C++Captain CharmiView Answer on Stackoverflow
Solution 5 - C++serupView Answer on Stackoverflow
Solution 6 - C++hfrmobileView Answer on Stackoverflow