10

I have used NUnit to run my C# unit tests successfully within Visual Studio 2013, with the NUnit GUI and with NUnit console.

For the first two I can get to my debug output (Console.Write(...)). In Visual Studio 2013, it is viewable by clicking the "output" link after the test and in the GUI the debug is displayed in the "Output" window.

When I run this with nunit3-console.exe I just get the summary report. I tried to look at what is in the XML output (in TestResults.xml), but that just contains more of the same summary report.

How can I get my debug to also come out on the screen?

My command line looks like this:

nunit3-console.exe "c:\path\to\my\assebly.dll" --trace=Verbose --test=Regression.Tests.HelloWorld

The test HelloWorld has the line Console.Write("Hello World\r\n"); in it, and I want to see this appear on the screen (standard output).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
code_fodder
  • 15,263
  • 17
  • 90
  • 167

1 Answers1

30

Your Console.WriteLine(...) should appear in the output, but in NUnit 3, you should use TestContext.WriteLine(...) in your tests. Because NUnit 3 is capable of running your tests in parallel, it captures that output and then prints it to the console when the test finishes running. That way, the output is matched with the test, not interleaved with other tests.

If you want your output to go out immediately when it is written, use TestContext.Progress.WriteLine(...). For example, the following test,

    [Test]
    public void ExampleOfConsoleOutput()
    {
        Console.WriteLine("Console.WriteLine In ExampleOfConsoleOutput");
        TestContext.WriteLine("TestContext.WriteLine In ExampleOfConsoleOutput");
        TestContext.Progress.WriteLine("TestContext.Progress.WriteLine In ExampleOfConsoleOutput");
    }

Outputs the following,

=> nunit.v3.TestNameInSetup.ExampleOfConsoleOutput
TestContext.Progress.WriteLine In ExampleOfConsoleOutput
Console.WriteLine In ExampleOfConsoleOutput
TestContext.WriteLine In ExampleOfConsoleOutput

Note that the Progress message was output before the other output even though it is last in the test.

You also don't need the --trace command line option. That is for NUnit internal tracing. The command line option to control output is --labels although the defaults (no command line options) show the above output.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rob Prouse
  • 22,161
  • 4
  • 69
  • 89
  • Re: Console.Write(...); ... you are right, the debug was coming out at the end of the test, I somehow missed that (I was expecting it to come out in real-time)...me being blind!. But what I really wanted was the "real-time" debug - and the TestContect.Progress.Write(...) works great! - thanks very much. – code_fodder Sep 26 '17 at 07:04
  • 1
    `TestContext.WriteLine` also worked with: Linux ([Ubuntu 20.04 MATE](https://en.wikipedia.org/wiki/Ubuntu_MATE#Releases) (Focal Fossa) base, with [Cinnamon](https://en.wikipedia.org/wiki/Cinnamon_(desktop_environment))), [.NET Core](https://en.wikipedia.org/wiki/.NET_Core) (3.1.5), and NUnit 3.12.0. – Peter Mortensen Jun 25 '20 at 10:29