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

C#ConsoleMstestWatin

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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");

Solution 2 - C#

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

Solution 3 - C#

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();
    }
}

Solution 4 - C#

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.

Solution 5 - C#

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");
 }
}

Solution 6 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJulianView Question on Stackoverflow
Solution 1 - C#Andras ZoltanView Answer on Stackoverflow
Solution 2 - C#chaliasosView Answer on Stackoverflow
Solution 3 - C#SimplyKnownAsGView Answer on Stackoverflow
Solution 4 - C#Gökhan KurtView Answer on Stackoverflow
Solution 5 - C#JJSView Answer on Stackoverflow
Solution 6 - C#riezeboschView Answer on Stackoverflow