I would like to write death tests for an application where exit code is 0 and there is some message displayed on cout instead of cerr.
void display_help(){
//std::cerr.set_rdbuf(std::cout.rdbuf());
//std::cerr << "From display_help" << std::endl;
std::cout << "From display_help"<< std::endl;
std::exit(0);
}
TEST_F(CommandlineTest, help_function_test) {
std::string exectedHelpMessage{ "" };
exectedHelpMessage += std::string("From display_help");
EXPECT_EXIT(display_help(),
::testing::ExitedWithCode(0),
::testing::ContainsRegex(exectedHelpMessage));
}
The above code fails because, its unable to match the message. My observation is, when the display_help function displays a message in cerr death test is able to capture. But, when the something is done on cout it's not able to.
Is there any suggestion on how such testing can be enabled ?