Printing additional output in Google Test
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.
Simply printing to stderr will work in the default test configuration.
std::cerr << "[ ] random seed = " << random_seed << std::endl;
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();