C++ 如何使用googletest捕获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);
参考

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

发布了291 篇原创文章 · 获赞 13 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/LU_ZHAO/article/details/105194934