Printing additional output in Google Test
Asked Answered
S

6

80

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.

Subteen answered 28/10, 2011 at 0:16 Comment(1)
I
44

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

std::cerr << "[          ] random seed = " << random_seed << std::endl;
Iolaiolande answered 27/1, 2014 at 19:32 Comment(5)
I did mention that in my question, and writing output this way does not include the usual coloured output.Subteen
std::cout doesnt work for me either but std:cerr showsSheaves
shouldn't it be std::cerr instead of std:cerr?Indemnity
this does not work for me, outputting to std::cerr still doesn't show.Snowstorm
This wasn't working for me because I was using ctest to run my tests, which hides the stdout. Running ctest -V to show the gtest output worked.Bustard
O
23

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.

Outlier answered 24/1, 2022 at 14:16 Comment(2)
Even better, the added lines will only be printed when the expectation fails. Nice!Ontine
What if you want to print an string output? the operator<< doesnt support conversion from string to intFinery
C
15

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();
Calder answered 17/4, 2012 at 10:47 Comment(0)
T
6

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.

Throughout answered 10/11, 2011 at 19:19 Comment(0)
A
5

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;
Alcoholic answered 3/12, 2017 at 0:14 Comment(1)
could not get this to work - no color changes - I have only seen this work on linuxFlew
F
2

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 [101;93m STYLES [0m
echo ^<ESC^>[0m [0mReset[0m
echo ^<ESC^>[1m [1mBold[0m
echo ^<ESC^>[4m [4mUnderline[0m
echo ^<ESC^>[7m [7mInverse[0m
echo.

enter image description here

Flew answered 14/4, 2021 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.