We are doing exactly what you are referring to.
First we created some macros:
#define CAPTURE_STDOUT StdoutRedirect::instance().redirect();
#define RELEASE_STDOUT StdoutRedirect::instance().reset();
#define ASSERT_INFO( COUNT, TARGET ) \
ASSERT_PRED_FORMAT2(OurTestPredicates::AssertInfoMsgOutput, TARGET, COUNT );
See this answer for capturing stdout and stderr:
https://mcmap.net/q/262955/-is-there-a-way-to-redirect-stdout-stderr-to-a-string
Just use their BeginCapture(), EndCapture() in place of our redirect() and reset().
In the AssertInfoMsgOutput method:
AssertionResult OurTestPredicates::AssertInfoMsgOutput( const char* TARGET,
const char* d1,
const char* d2,
int COUNT )
{
int count = 0;
bool match = false;
std::string StdOutMessagge = GetCapture();
// Here is where you process the stdout/stderr info for the TARGET, and for
// COUNT instances of that TARGET message, and set count and match
// appropriately
...
if (( count == COUNT ) && match )
{
return ::testing::AssertionSuccess();
}
return :: testing::AssertionFailure() << "not found";
}
Now in your unit test just wrap your calls that you want to capture stdout/stderr with:
CAPTURE_STDOUT
// Make your call to your code to test / capture here
ASSERT_INFO( 1, "Foo bar" );
RELEASE_STDOUT
ostream
interface would make it easier for example. – Unyoke