65

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
sgtz
  • 8,849
  • 9
  • 51
  • 91
  • For one of the answers, it is the same as in *[How do I get nunit3-console to output my debug to screen?](https://stackoverflow.com/questions/46405753/how-do-i-get-nunit3-console-to-output-my-debug-to-screen-on-a-windows-box)* - *"in NUnit 3, you should use TestContext.WriteLine(...) in your tests"*. It [works on Linux](https://stackoverflow.com/questions/46405753/how-do-i-get-nunit3-console-to-output-my-debug-to-screen-on-a-windows-box#comment110657051_46407376) as well. – Peter Mortensen Jul 10 '23 at 09:35

5 Answers5

72

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

TestContext.Out.WriteLine("Message to write to log");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ashitakalax
  • 1,437
  • 1
  • 18
  • 26
  • This is the type of functionality I was looking for. Works in Debug *and* Release. Visual Studio 2017(15.8.7) – Aaron Jordan Oct 19 '18 at 21:26
  • 6
    Even better is TestContext.Progress.WriteLine(string) for continuous output to the Output window of Visual Studio 2017. – Pebermynte Lars Oct 03 '19 at 15:43
  • 1
    I don't get anything in output window when running a unit test nor in the output log – John Demetriou May 06 '20 at 12:07
  • 2
    Or, even shorter [`TestContext.Write(...)`](https://docs.nunit.org/articles/nunit/writing-tests/TestContext.html#write) and [`TestContext.WriteLine(...)`](https://docs.nunit.org/articles/nunit/writing-tests/TestContext.html#writeline). – nevermind Mar 15 '21 at 12:24
  • Where the output will show in VS Code? – tarekahf Mar 19 '22 at 04:22
  • I'm not sure on VS Code, this was focused on Visual Studio's. However, you may check the Test Explorer log. That might have it, or some similar option. – Ashitakalax Mar 21 '22 at 21:05
56

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

Pang
  • 9,564
  • 146
  • 81
  • 122
Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130
  • 3
    thanks for pointing this out. I now realise that I need to have nunit installed. Currently it is running via an assembly reference that came with the project, and I think resharper has hooks into it. – sgtz Jul 26 '11 at 17:07
  • 1
    @sgtz Are you running your NUnit tests via the ReSharper test runner? If so, the output should be in the output window of the test runner. Please see my update. – Tim Lloyd Jul 26 '11 at 17:12
35

Try using System.Diagnostics.Debug.WriteLine instead.

Paul Creasey
  • 28,321
  • 10
  • 54
  • 90
15

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LostNomad311
  • 1,975
  • 2
  • 23
  • 31
9

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

MrClan
  • 6,402
  • 8
  • 28
  • 43