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

发布于 2024-10-15 08:05:14 字数 598 浏览 6 评论 0原文

上下文:
我们有一些用户报告我们的网络应用程序中的文件上传功能存在问题。它只是偶尔发生,没有任何特殊模式。很长一段时间以来,我们一直在尝试解决这个问题,在我们认为可能有帮助的任何地方添加调试信息,爬行日志等,但我们一直无法重现或解决它。

问题:
我现在尝试通过使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

-残月青衣踏尘吟 2024-10-22 08:05:14

控制台输出未出现是因为后端代码未在测试上下文中运行。

您可能最好使用 Trace.WriteLine (在 System.Diagnostics 中),然后添加一个写入文件的跟踪侦听器。

MSDN 中的此主题展示了一种执行此操作的方法。


根据马蒂·尼尔和戴夫·安德森的评论:

使用系统;
使用系统诊断;

...

Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
// 或 Trace.Listeners.Add(new ConsoleTraceListener());
Trace.WriteLine("你好世界");

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:

using System;
using System.Diagnostics;

...

Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
// or Trace.Listeners.Add(new ConsoleTraceListener());
Trace.WriteLine("Hello World");
傾城如夢未必闌珊 2024-10-22 08:05:14

使用Debug.WriteLine。这将立即在Output 窗口中显示您的消息。唯一的限制是您必须在调试模式下运行测试。

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

输出

在此处输入图像描述

Use the Debug.WriteLine. This will display your message in the Output window immediately. The only restriction is that you must run your test in Debug mode.

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

Output

enter image description here

雅心素梦 2024-10-22 08:05:14

我找到了自己的解决方案。我知道 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"));
    }
}

一次性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 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.

[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:

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();
    }
}
凉城已无爱 2024-10-22 08:05:14

我遇到了同样的问题,并且我正在“运行”测试。如果我改为“调试”测试,调试输出将像所有其他跟踪和控制台一样显示得很好。
如果您“运行”测试,我不知道如何查看输出。

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.

简美 2024-10-22 08:05:14

它不是控制台,而是在输出面板中。

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

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

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");
 }
}
北座城市 2024-10-22 08:05:14

更新:负载测试功能为已弃用

--- 原始答案
您最好设置一个测试并根据该测试创建性能测试。这样您就可以使用默认工具集监控进度。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文