将 cout 和 stdout 都重定向到 C++ 中的字符串以进行单元测试

Redirect both cout and stdout to a string in C++ for Unit Testing(将 cout 和 stdout 都重定向到 C++ 中的字符串以进行单元测试)
本文介绍了将 cout 和 stdout 都重定向到 C++ 中的字符串以进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正致力于在单元测试中获取一些遗留代码,有时感知现有程序行为的唯一方法是通过控制台输出.

我在网上看到了很多关于如何将标准输出重定向到 C++ 中的另一个文件的示例,但是有没有一种方法可以将其重定向到内存中的流,这样我的测试就不必依赖磁盘了?

>

我想将遗留代码发送到 stdout 的任何内容放入 std::string 中,以便我可以轻松地在输出中找到.

编辑

遗留代码太糟糕了,它使用了 cout <<..printf.这是我目前所拥有的:

void TestSuite::setUp(void){oldStdoutBuf = std::cout.rdbuf();std::cout.rdbuf(consoleOutput.rdbuf());}void TestSuite::tearDown(void){std::cout.rdbuf(oldStdoutBuf);}

问题是这不会使用 printf 捕获输出.我想要两者兼而有之的东西.有什么想法吗?

解决方案

std::stringstream 可能就是你要找的.

更新
好吧,这是一个小技巧,但也许你可以这样做来获取 printf 输出:

char huge_string_buf[MASSIVE_SIZE];freopen("NUL", "a", 标准输出);setbuf(标准输出,巨大的字符串缓冲区);

请注意,Linux 应使用/dev/null"而不是NUL".这将迅速开始填满 big_string_buffer.如果您希望能够在缓冲区已满后继续重定向输出,则必须调用 fflush(),否则会引发错误.有关详细信息,请参阅 std::setbuf.

I'm working on getting some legacy code under unit tests and sometimes the only way to sense an existing program behavior is from the console output.

I see lots of examples online for how to redirect stdout to another file in C++, but is there a way I can redirect it to an in-memory stream so my tests don't have to rely on the disk?

I'd like to get anything that the legacy code sends to stdout into a std::string so I can easily .find on the output.

Edit

The legacy code is so bad that it users a mixture of cout << .. and printf. Here is what I have so far:

void TestSuite::setUp(void)
{
    oldStdoutBuf = std::cout.rdbuf();
    std::cout.rdbuf(consoleOutput.rdbuf());
}
void TestSuite::tearDown(void)
{
    std::cout.rdbuf(oldStdoutBuf);
}

The problem is that this does not capture output using printf. I would like something that gets both. Any ideas?

解决方案

std::stringstream may be what you're looking for.

UPDATE
Alright, this is a bit of hack, but maybe you could do this to grab the printf output:

char huge_string_buf[MASSIVE_SIZE];
freopen("NUL", "a", stdout);
setbuf(stdout, huge_string_buffer);

Note you should use "/dev/null" for linux instead of "NUL". That will rapidly start to fill up huge_string_buffer. If you want to be able to continue redirecting output after the buffer is full you'll have to call fflush(), otherwise it will throw an error. See std::setbuf for more info.

这篇关于将 cout 和 stdout 都重定向到 C++ 中的字符串以进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Bring window to front -gt; raise(),show(),activateWindow() don’t work(把窗户放在前面 -raise(),show(),activateWindow() 不起作用)
How to get a list video capture devices NAMES (web cameras) using Qt (crossplatform)? (C++)(如何使用 Qt(跨平台)获取列表视频捕获设备名称(网络摄像机)?(C++))
How to compile Qt as static(如何将 Qt 编译为静态)
C++ over Qt : Controlling transparency of Labels and Buttons(C++ over Qt:控制标签和按钮的透明度)
How to know when a new USB storage device is connected in Qt?(Qt如何知道新的USB存储设备何时连接?)
What is an event loop in Qt?(Qt 中的事件循环是什么?)