C++ 如何在 Visual Studio 2012 单元测试中获取控制台输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16815804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get console output in Visual Studio 2012 Unit Tests
提问by bradgonesurfing
I have a managed C++ unit test in VS 2012. The test runs fine and I can
verify that a loop with multiple cout
calls is executed.
我在 VS 2012 中有一个托管 C++ 单元测试。测试运行良好,我可以验证是否cout
执行了具有多个调用的循环。
However when I look at the test explorer the test is marked as passed but there is no hyper link for the output as I am used to for c# projects.
但是,当我查看测试资源管理器时,测试被标记为通过,但没有像我习惯的 c# 项目那样输出超链接。
The code at the end of my test is
我测试结束时的代码是
for (int i = 0; i < 4; i++)
{
cout << parameters[i];
cout << endl;
}
which I can verify runs as I step through
it in the debugger. I have also tried with
cerr
but no difference.
当我在调试器中逐步执行时,我可以验证它的运行情况。我也试过,
cerr
但没有区别。
回答by olen_garn
You can use Debug::WriteLine() (in the System::Diagnostics namespace) or Console::WriteLine() to write output to the Visual Studio 2012 console.
您可以使用 Debug::WriteLine()(在 System::Diagnostics 命名空间中)或 Console::WriteLine() 将输出写入 Visual Studio 2012 控制台。
Code for the test (note that the System::Diagnostics namespace is declared elsewhere).
测试代码(注意 System::Diagnostics 命名空间是在别处声明的)。
The test result view.
测试结果视图。
After clicking the "Output" link:
单击“输出”链接后:
It is not using std::cout, but hopefully this will do what you need it to do.
它没有使用 std::cout,但希望这能满足您的需求。
回答by Hanan N.
For me seems to work using:
对我来说似乎可以使用:
Logger::WriteMessage("What ever message");
Logger::WriteMessage("What ever message");
After you run the test you can see the output in the Test Explorer
window by clicking on output
运行测试后,您可以Test Explorer
通过单击在窗口中看到输出output
回答by Prof Von Lemongargle
I don't know that I can give you a definitive answer, but I may be able to provide a clue.
我不知道我可以给你一个明确的答案,但我也许可以提供一个线索。
In my older code that needed to get output to the console window during a custom build step, I used the following lines:
在我需要在自定义构建步骤期间将输出输出到控制台窗口的旧代码中,我使用了以下几行:
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
There is a description at http://msdn.microsoft.com/en-us/library/8hyw4sy7(v=vs.71).aspxfor _CrtDbgReport.
http://msdn.microsoft.com/en-us/library/8hyw4sy7(v=vs.71).aspx 上有 _CrtDbgReport的描述。
For me, this made the output from my managed C++ show up through the build output window. Hope it can help you with Unit Testing.
对我来说,这使得我的托管 C++ 的输出通过构建输出窗口显示出来。希望它可以帮助您进行单元测试。
回答by n8CodeGuru
According to Microsoft connect trx and test results are deprecated
根据 Microsoft connect trx 和测试结果已弃用
:(
:(