如何在执行 MSTest 测试期间写入 Console.Out
上下文:
我们有一些用户报告我们的网络应用程序中的文件上传功能存在问题。它只是偶尔发生,没有任何特殊模式。很长一段时间以来,我们一直在尝试解决这个问题,在我们认为可能有帮助的任何地方添加调试信息,爬行日志等,但我们一直无法重现或解决它。
问题:
我现在尝试通过使用 MSTest 和 WatiN 重复应该失败多次(数百次)的操作来重现此情况。只是为了了解测试在循环中进行了多远,我想打印如下内容:
Console.WriteLine(String.Format("Uploaded file, attempt {0} of {1}", i, maxUploads));
但是,这不会出现在“输出”窗口中。现在我知道您将在测试结果中获得控制台输出(以及从 Debug.Writeline 等输出的内容),但是直到之后之后才可用测试已经完成。由于我的数百次重复测试可能需要相当长的时间,我想知道它已经进展到什么程度了。
问题:
有没有办法可以在测试执行期间在输出窗口中获取控制台输出?
Context:
We have some users reporting issues with a file upload feature in our web application. It only happens occasionally and without any special pattern. We have been trying to figure it out for a long time, adding debug information anywhere we can think it might help, crawling the logs etc, but we have not been able to reproduce or figure it out.
Problem:
I'm now trying to reproduce this by using MSTest and WatiN to repeat the operation that is supposed to fail a large number of times (several hundreds). Just to have a clue about how far in the loop the test has gotten, I want to print something like:
Console.WriteLine(String.Format("Uploaded file, attempt {0} of {1}", i, maxUploads));
This does however not appear in the Output window. Now I know that you'll get the console output in the test results (as well as what you output from Debug.Writeline
etc), but this is not available until after the test has finished. And since my test with hundreds of repetitions could take quite some time, I'd like to know how far it has gotten.
Question:
Is there a way I can get the console output in the Output window during test execution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
控制台输出未出现是因为后端代码未在测试上下文中运行。
您可能最好使用
Trace.WriteLine
(在 System.Diagnostics 中),然后添加一个写入文件的跟踪侦听器。MSDN 中的此主题展示了一种执行此操作的方法。
根据马蒂·尼尔和戴夫·安德森的评论:
The Console output is not appearing is because the backend code is not running in the context of the test.
You're probably better off using
Trace.WriteLine
(In System.Diagnostics) and then adding a trace listener which writes to a file.This topic from MSDN shows a way of doing this.
According to Marty Neal's and Dave Anderson's comments:
使用
Debug.WriteLine
。这将立即在Output
窗口中显示您的消息。唯一的限制是您必须在调试
模式下运行测试。输出
Use the
Debug.WriteLine
. This will display your message in theOutput
window immediately. The only restriction is that you must run your test inDebug
mode.Output
我找到了自己的解决方案。我知道 Andras 的答案可能与 MSTEST 最一致,但我不想重构我的代码。
一次性
ConsoleRedirector
定义为:I found a solution of my own. I know that Andras answer is probably the most consistent with MSTEST, but I didn't feel like refactoring my code.
The disposable
ConsoleRedirector
is defined as:我遇到了同样的问题,并且我正在“运行”测试。如果我改为“调试”测试,调试输出将像所有其他跟踪和控制台一样显示得很好。
如果您“运行”测试,我不知道如何查看输出。
I had the same issue and I was "Running" the tests. If I instead "Debug" the tests the Debug output shows just fine like all others Trace and Console.
I don't know though how to see the output if you "Run" the tests.
它不是控制台,而是在输出面板中。
It's not the console, but it is in the output panel.
更新:负载测试功能为已弃用。
--- 原始答案
您最好设置一个测试并根据该测试创建性能测试。这样您就可以使用默认工具集监控进度。
Update: Load Tests functionality is deprecated.
--- original answer
You better setup a single test and create a performance test from this test. This way you can monitor the progress using the default tool set.