Build/Test Issues

Setting Console output and error report

Started by Vincent on 3,283 views

I have the following NUnit test:

[Test]
public void Test()
{
StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
Assert.That(1, Is.EqualTo(2));
}

The NCrunch Tests window reports that the test fails, but doesn't show me the assert error.

If I remove the console redirection, it works, the assert error is displayed.

How do I restore the output so that the assert error is displayed ?

(Of course, I am doing real stuff with the Console redirection, I simplified the example).

Using VS2015 Professional update 1 and Nunit 3.8.1
Hi, thanks for sharing this issue.

Exceptions from failed assertions are output to NCrunch using the console output stream. To capture all exceptions thrown in an environment and all other data, NCrunch redirects the console output stream to its own internal buffer. By redirecting it yourself, you override NCrunch's own behaviour and capture the data yourself.

There isn't really much we can do about this. NCrunch can't coherently capture trace data without its console output stream in place. You'll still receive exceptions inline in your code coverage and the test will still fail, but no data will be reported for the test inside the Tests Window.
I found a workaround: wrapping the assert in a try catch, writing the exception to the Console.Error output stream and rethrowing it:

try
{
    StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput());
    standardOutput.AutoFlush = true;
    Console.SetOut(standardOutput);
    Assert.That(1, Is.EqualTo(2));
}
catch(Exception e)
{
    Console.Error.WriteLine(e.ToString());
    throw;
}

Edited

Post a reply

Log in to reply.