c++ - How to capture stdout/stderr with googletest? -


Is it possible to capture stdout and stderr when using framework?

For example, I have to call the function writing the errors in the stderr. Now, while calling the function in the test, I want to claim that there is no output available. Or, maybe I want to check error behavior and I would like to say that a certain string gets printed when I (deliberately) generate an error.

I used this snippet before redirecting the cout call to a stringstream product . Hopefully it can spark some ideas I've never used googletest before.

  // This can be an offstream or another ostream std :: stringstream buffer; // save the quotas buffer here std :: streambuf * sbuf = std :: cout.rdbuf (); // Redirect cout to our stringstream buffer or any other ostream studio :: cout.rdbuf (buffer .rdf); // as usual stout :: cout & lt; & Lt; "Hello world"; // When your old self std :: cout.rdbuf (sbuf) redirect cout;  

Use your Google test before redirecting to the original output to check the output in the buffer.


Comments