The solution should not hard code the output stream. Pass the link to std::ostreamyour code somehow and use it std::stringstreamto collect the result in a test environment.
For example, this is the contents of your “other .cpp” file:
void toBeTested(std::ostream& output) {
output << "this is the correct output";
}
, / std::cout :
void productionCode() {
toBeTested(std::cout);
}
sting :
#include <sstream>
#include <cassert>
void test() {
std::stringstream ss;
toBeTested(ss);
assert(ss.str() == "this is the correct output");
}