How to use C ++ googletest capture stdout / stderr?

// This can be an ofstream as well or any other ostream
std::stringstream buffer;

// Save cout's buffer here
std::streambuf *sbuf = std::cout.rdbuf();

// Redirect cout to our stringstream buffer or any other ostream
std::cout.rdbuf(buffer.rdbuf());

// Use cout as usual
std::cout << "Hello World";

// When done redirect cout to its old self
std::cout.rdbuf(sbuf);
reference

https://stackoverflow.com/questions/3803465/how-to-capture-stdout-stderr-with-googletest

Published 291 original articles · won praise 13 · views 80000 +

Guess you like

Origin blog.csdn.net/LU_ZHAO/article/details/105194934