- It turns out the library has a very nice tool for this.
- reference
- You need to include
-
#include <boost/test/tools/output_test_stream.hpp>
- The namespace is rather complex, so this will help too
-
using boost::test_tools::output_test_stream;
- Declare a test stream, and use some tests.
-
output_test_stream output;
string errorMessage = "Error: Attempt to access Empty List.\n";
- Note the \n added to the string, this represents the endl that was printed with the message.
- Some C++ magic, replace the output stream with the new stream.
-
// change stdout to be the new output buffer
streambuf * old = cout.rdbuf(output.rdbuf());
// force the error
string x = aList.Data();
// restore cout.
cout.rdbuf(old);
- Finally, run the output tests
-
BOOST_TEST(output.is_equal(errorMessage,false));
BOOST_TEST(output.check_length(errorMessage.size(), false));
BOOST_TEST(!output.is_empty(false));
- Note the parameter bool to all commands,
- This tells boost not to clear the buffer.
- The framework uses cout, so make sure you don't have the framework output anything while capturing I/O.
- Please, not, this is hard to recreate, you probably want to save this somewhere.