googletest - 用于捕获 cout 的 gtest 死亡测试
问题描述
我想为退出代码为 0 并且在 cout 而不是 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));
}
上面的代码失败,因为它无法匹配消息。我的观察是,当 display_help 函数在 cerr 死亡测试中显示消息时是能够捕获的。但是,当某事在 cout 上完成时,它就无法做到。
关于如何启用此类测试有什么建议吗?
解决方案
推荐阅读
- node.js - NodeJs - 如何在 NodeJs 中获取 Windows ProgramData 目录
- python - 在 Python 中查找半素数的因式
- python - Matplotlib 多个颜色条出现在子图动画中
- python - 如何使用 read_sql_query 读取带十进制逗号的浮点值
- c# - 使用 Oauth2 通过 Office365 C# 发送电子邮件
- docker - 从没有 .dockerignore 的 docker build COPY 命令中排除文件夹
- r - 如何在 ggplot 中添加统计值?
- excel - 当用户关闭标签或在特定单元格中按 Enter 时运行宏
- wso2 - WSO2IS 5.3.0 - 联合身份验证可以创建本地会话吗?
- gnuplot - 如何在 gnuplot 中绘制这种数据?