简体   繁体   English

如何在执行 MSTest 测试期间写入 Console.Out

[英]How to write to Console.Out during execution of an MSTest test

Context:语境:
We have some users reporting issues with a file upload feature in our web application.我们有一些用户报告我们的 Web 应用程序中的文件上传功能存在问题。 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).我现在试图通过使用 MSTest 和 WatiN 来重复这个应该失败很多次(几百次)的操作。 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.现在我知道您将在测试结果中获得控制台输出(以及您从Debug.Writeline等输出的内容),但这测试完成之前不可用。 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?有没有办法在测试执行期间在“输出”窗口中获取控制台输出?

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. 您最好使用Trace.WriteLine (在System.Diagnostics中),然后添加一个写入文件的跟踪侦听器。

This topic from MSDN shows a way of doing this. 来自MSDN的主题显示了一种实现方法。


According to Marty Neal's and Dave Anderson's comments: 根据Marty Neal和Dave Anderson的评论:

 using System; using System.Diagnostics; ... Trace.Listeners.Add(new TextWriterTraceListener(Console.Out)); // or Trace.Listeners.Add(new ConsoleTraceListener()); Trace.WriteLine("Hello World"); 

Use the Debug.WriteLine . 使用Debug.WriteLine This will display your message in the Output window immediately. 这将立即在“ Output窗口中显示您的消息。 The only restriction is that you must run your test in Debug mode. 唯一的限制是您必须在“ Debug模式下运行测试。

[TestMethod]
public void TestMethod1()
{
    Debug.WriteLine("Time {0}", DateTime.Now);
    System.Threading.Thread.Sleep(30000);
    Debug.WriteLine("Time {0}", DateTime.Now);
}

Output 输出量

在此处输入图片说明

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. 我知道Andras的答案可能与MSTEST最一致,但是我不想重构代码。

[TestMethod]
public void OneIsOne()
{
    using (ConsoleRedirector cr = new ConsoleRedirector())
    {
        Assert.IsFalse(cr.ToString().Contains("New text"));
        /* call some method that writes "New text" to stdout */
        Assert.IsTrue(cr.ToString().Contains("New text"));
    }
}

The disposable ConsoleRedirector is defined as: 一次性的ConsoleRedirector定义为:

internal class ConsoleRedirector : IDisposable
{
    private StringWriter _consoleOutput = new StringWriter();
    private TextWriter _originalConsoleOutput;
    public ConsoleRedirector()
    {
        this._originalConsoleOutput = Console.Out;
        Console.SetOut(_consoleOutput);
    }
    public void Dispose()
    {
        Console.SetOut(_originalConsoleOutput);
        Console.Write(this.ToString());
        this._consoleOutput.Dispose();
    }
    public override string ToString()
    {
        return this._consoleOutput.ToString();
    }
}

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. 如果我改为“调试”测试,则Debug输出将像其他所有Trace和Console一样正常显示。 I don't know though how to see the output if you "Run" the tests. 如果您“运行”测试,我不知道如何查看输出。

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. 这样,您可以使用默认工具集监视进度。

It's not the console, but it is in the output panel.它不是控制台,而是在输出面板中。

public class Test
{
 public TestContext TestContext { get; set; }

 [TestMethod]
 public void Foo()
 {
   TestContext.WriteLine("Hello World");
 }
}

微软官方团队在这里给出了解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM