Replace Console.WriteLine in NUnit

C#.NetDebuggingNunit

C# Problem Overview


I haven't done much with NUnit before, but I just wanted to dump some text to a window in a console type fashion. For example:

Console.WriteLine("... some information...");

That won't work of course because NUnit is driving things.

I'm in the middle of building some unit tests and want to dump a list of variable values for inspection during debug. It isn't strictly a unit test if I need to do this, I admit that, but it would be convenient.

C# Solutions


Solution 1 - C#

In NUnit v3, you can also write to the test results output for debugging with

TestContext.Out.WriteLine("Message to write to log");

Solution 2 - C#

You can see the console output. You just have to select the "Text Output" tab in the NUnit GUI runner.

Enter image description here

If you are using the ReSharper test runner, the console output should be displayed. Ensure that the test runner output window is displayed by clicking the "Show Output" button in the test runner tool bar:

Enter image description here

You should then get something as follows:

Enter image description here

Solution 3 - C#

Try using System.Diagnostics.Debug.WriteLine instead.

Solution 4 - C#

In Visual Studio 2017, in the Test Explorer window, there is a link, Output, in the lower Test pane. This brings up anything written to the console during that unit test.

Solution 5 - C#

Use the Test-Output view.

Steps:

  1. Open Test Explorer
  2. Select any particular test
  3. Run it if it has never been run.
  4. Click on the output link on the test results pane.

There is no need to replace Console.WriteLine with anything as this view logs messages from:

> Console.WriteLine > > Trace.WriteLine > > Debug.WriteLine > > TestExplorer.Out.WriteLine

Enter image description here

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
QuestionsgtzView Question on Stackoverflow
Solution 1 - C#AshitakalaxView Answer on Stackoverflow
Solution 2 - C#Tim LloydView Answer on Stackoverflow
Solution 3 - C#Paul CreaseyView Answer on Stackoverflow
Solution 4 - C#LostNomad311View Answer on Stackoverflow
Solution 5 - C#MrClanView Answer on Stackoverflow