165

Any call in my unit tests to either Debug.Write(line) or Console.Write(Line) simply gets skipped over while debugging and the output is never printed. Calls to these functions from within classes I'm using work fine.

I understand that unit testing is meant to be automated, but I still would like to be able to output messages from a unit test.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris
  • 2,341
  • 5
  • 23
  • 21
  • For [NUnit](https://en.wikipedia.org/wiki/NUnit): *[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:38

17 Answers17

165

Try using TestContext.WriteLine() which outputs text in test results.

Example:

[TestClass]
public class UnitTest1
{
    private TestContext testContextInstance;

    /// <summary>
    /// Gets or sets the test context which provides
    /// information about and functionality for the current test run.
    /// </summary>
    public TestContext TestContext
    {
        get { return testContextInstance; }
        set { testContextInstance = value; }
    }

    [TestMethod]
    public void TestMethod1()
    {
        TestContext.WriteLine("Message...");
    }
}

The "magic" is described in MSDN:

To use TestContext, create a member and property within your test class [...] The test framework automatically sets the property, which you can then use in unit tests.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
frennky
  • 12,581
  • 10
  • 47
  • 63
  • 9
    I have found (with VS2013) that this only prints something if the test is run in debug mode. – fusi Sep 24 '15 at 11:15
  • 3
    It seems that using TestContext requires VS2015 Enterprise (or premium versions of earlier editions), according to [this documentation](https://msdn.microsoft.com/en-us/library/dd465178(v=vs.140).aspx) – Patrick Steadman Nov 24 '15 at 21:48
  • I'm noticing that if your string has curly braces in it, the method blows up. So "_testContext.WriteLine("hello");" works but "_testContext.WriteLine("he{ll}o");" fails with "System.FormatException: Input string was not in a correct format." – Mike K Jul 20 '17 at 21:08
  • 1
    The WriteLine method in TestContext takes the same parameters that Console.WriteWriteLine takes. That means that the string is a formatting string (documented at https://learn.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting). To use a literal { in a string, you need to double it. To print your string use WriteLine("he{{ll}}o"); – Walter Nov 07 '17 at 21:59
  • 1
    this was interesting... although i still don't know why they didn't add this and similar features to a base class or interface we could use from the framework to avoid confusion. – Niklas Apr 16 '19 at 05:49
  • In VS 2017, I see `Debug`, `Console`, and `TestContext` `WriteLine`s all in the test output – xr280xr Aug 01 '20 at 01:54
164

I was also trying to get Debug or Trace or Console or TestContext to work in unit testing.

None of these methods would appear to work or show output in the output window:

    Trace.WriteLine("test trace");
    Debug.WriteLine("test debug");
    TestContext.WriteLine("test context");
    Console.WriteLine("test console");

Visual Studio 2012 and greater

(from comments) In Visual Studio 2012, there is no icon. Instead, there is a link in the test results called Output. If you click on the link, you see all of the WriteLine.

Prior to Visual Studio 2012

I then noticed in my Test Results window, after running the test, next to the little success green circle, there is another icon. I doubled clicked it. It was my test results, and it included all of the types of writelines above.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yogurt The Wise
  • 4,379
  • 4
  • 34
  • 42
  • 44
    In Visual Studio 2012, there is no icon. Instead, there is a link in the test results called "Output". If you click on the link, you see all of the writelines. – epotter Apr 25 '13 at 19:20
  • 18
    geez, that "Output" link is intrinsically hard to find. If anyone is struggling to find it, it's in the Text Explorer, bottom section. When a test is selected, it shows you the result with "Elapsed time: xxx". Below that is the "Output" link. – kevin May 18 '14 at 11:00
  • @kevin, in which version of VS did you see that? (I'm assuming you mean "Test Explorer" instead of "Text Explorer.) I don't see an Output link in VS2013 express. – Mike C Jul 10 '15 at 21:24
  • 1
    I don't see an Output link either (using VS2013 Pro, XUnit, Resharper) – atreeon Jul 25 '15 at 08:24
  • I've just tested in VS2013 Pro and the link doesn't appear unless that test has written something to output (I used Console.WriteLine()). – christutty Jan 14 '16 at 02:34
  • 1
    If anyone wonders (like me) which of these appear in a TRX (test results output) file then it's all of the above except "Debug.WriteLine". – Badgerspot Aug 06 '16 at 21:40
  • 4
    In Visual Studio 2017 it is still the 'Output' link. – HankCa Jan 08 '18 at 03:20
  • For those didn't see "output" link. 1) You should select a test **method**. 2) let the Test Explorer window fill the VS window vertically otherwise the link was folded and the scroll bar is too small to be noticed or used. – Meow Cat 2012 Jul 24 '19 at 04:55
  • 7
    There is no longer an output link in VS2022 – Kyle Delaney Aug 19 '21 at 16:25
  • VS 2022 onwards: They put the output where the link used to be. There is a Section called "Standard Output" now – k29 Jul 22 '23 at 18:53
81

In Visual Studio 2017, you can see the output from test explorer.

1) In your test method, Console.WriteLine("something");

2) Run the test.

3) In Test Explorer window, click the Passed Test Method.

4) And click the "Output" link.

enter image description here

And click "Output", you can see the result of Console.Writeline(). enter image description here

monad.gon
  • 917
  • 7
  • 12
  • 1
    Excellent. Visual Studio/C# is not my norm for developing, this is just want I needed! Thank you for posting this. – matt32 Jun 02 '18 at 01:00
  • 15
    I am using 2017 Version 15.5.6 Bit O don't see the output link. – Mike IT Jul 12 '18 at 17:56
  • 1
    Does it depend on which test framework is in use? I'm using xUnit 2.4 in a .NET 4.6 test project and no "Standard Output" pane appears. Output from `Console.WriteLine` is not visible in the Output pane under "Tests" either. – Qwertie Jun 04 '19 at 15:00
  • For xUnit, jonzim's answer worked for me, even in a different thread spawned by the test. – Qwertie Jun 04 '19 at 15:31
  • 2
    For those didn't see "output" link. 1) You should select a test **method**. 2) let the Test Explorer window fill the VS window vertically otherwise the link was hidden and the scroll bar is too small to be noticed or used. – Meow Cat 2012 Jul 24 '19 at 04:55
  • The output link no longer appears for me in Visual Studio 16.2 and 16.2.2. Does anyone know if this feature has been removed or if the various "WriteLine" methods that were supported before are no longer supported? – Tyree Jackson Aug 19 '19 at 21:07
  • This also applies to VS2022, at least Pro. – Saplu Jan 31 '23 at 06:50
35

It depends on your test runner... for instance, I'm using xUnit, so in case that's what you are using, follow these instructions:

https://xunit.net/docs/capturing-output

This method groups your output with each specific unit test.

using Xunit;
using Xunit.Abstractions;

public class MyTestClass
{
    private readonly ITestOutputHelper output;

    public MyTestClass(ITestOutputHelper output)
    {
        this.output = output;
    }

    [Fact]
    public void MyTest()
    {
        var temp = "my class!";
        output.WriteLine("This is output from {0}", temp);
    }
}

There's another method listed in the link I provided for writing to your Output window, but I prefer the previous.

mmizutani
  • 317
  • 2
  • 9
jonzim
  • 446
  • 5
  • 4
9

In VS 2019

  1. in the main VS menu bar click: View -> Test Explorer
  2. Right-click your test method in Test Explorer -> Debug
  3. Click the additional output link as seen in the screenshot below.

enter image description here

You can use:

  • Debug.WriteLine
  • Console.WriteLine
  • TestContext.WriteLine

all will log to the additional output window.

Legends
  • 21,202
  • 16
  • 97
  • 123
  • 1
    Can't see console output in [`OneTimeSetUp`](https://docs.nunit.org/articles/nunit/writing-tests/attributes/onetimesetup.html) and [`OneTimeTearDown`](https://docs.nunit.org/articles/nunit/writing-tests/attributes/onetimeteardown.html) though. – Pang Dec 29 '20 at 08:31
  • When I right click and select debug it reruns the test in debug mode. I'm using NUnit3, what test framework are you using? – RonC Feb 01 '21 at 23:27
  • 1
    Looks like autofac while using a test adapter for MsTest https://www.automatetheplanet.com/mstest-cheat-sheet/#tab-con-6 – Legends Feb 02 '21 at 08:56
4

I think it is still actual.

You can use this NuGet package: Bitoxygen.Testing.Pane

Call the custom WriteLine method from this library. It creates a Testing pane inside the Output window and puts messages there always (during each test, it runs independently of DEBUG and TRACE flags).

To make tracing more easy I can recommend to create a base class:

[TestClass]
public abstract class BaseTest
{
    #region Properties
    public TestContext TestContext { get; set; }

    public string Class
    {
        get { return this.TestContext.FullyQualifiedTestClassName; }
    }
    public string Method
    {
        get { return this.TestContext.TestName; }
    }
    #endregion

    #region Methods
    protected virtual void Trace(string message)
    {
        System.Diagnostics.Trace.WriteLine(message);

        Output.Testing.Trace.WriteLine(message);
    }
    #endregion
}

[TestClass]
public class SomeTest : BaseTest
{
    [TestMethod]
    public void SomeTest1()
    {
        this.Trace(string.Format("Yeah: {0} and {1}", this.Class, this.Method));
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Maxim
  • 13,029
  • 6
  • 30
  • 45
  • 1
    The magic is at: Output.Testing.Trace.WriteLine(message); with BitOxygen. – Bimal Poudel Sep 30 '16 at 17:43
  • 1
    Does not work with the current Visual Studio version you get a error `Could not load file or assembly 'Microsoft.VisualStudio.Shell.12.0, ` – quadroid Jul 09 '18 at 09:59
  • @Console Yes it needs some support, but I am not sure if community is interesting in this way to output. xUnit has OutputHandler and VS can show its result. – Maxim Jul 06 '20 at 15:35
4

It is indeed depending on the test runner as @jonzim mentioned. For NUnit 3 I had to use NUnit.Framework.TestContext.Progress.WriteLine() to get running output in the Output window of Visual Studio 2017.

NUnit describes how to: here

To my understanding this revolves around the added parallelization of test execution the test runners have received.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pebermynte Lars
  • 414
  • 3
  • 14
3

I'm using xUnit so this is what I use:

Debugger.Log(0, "1", input);

PS: you can use Debugger.Break(); too, so you can see your log in out.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ivandro Jao
  • 2,731
  • 5
  • 24
  • 23
2

I've accidentally found that DebugView (with enabled Capture > Capture Win32 option) will capture writes to System.Console.

Example test:

[Test]
public void FooTest()
{
    for (int i = 0; i < 5; i++)
        Console.WriteLine($"[{DateTime.UtcNow.ToString("G")}] Hello World");
}

enter image description here

oleksii
  • 35,458
  • 16
  • 93
  • 163
2

If you're running tests with dotnet test, you may find that Console.Error.WriteLine produces output.

This is how I've worked around the issue with NUnit tests, which also seem to have all Debug, Trace and standard output suppressed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
daf
  • 1,289
  • 11
  • 16
  • 1
    I can confirm that. Tried with .NET Core 3.1 on [Ubuntu MATE 20.04](https://en.wikipedia.org/wiki/Ubuntu_MATE#Releases) (Focal Fossa). `System.Console.Error.WriteLine` worked, but `System.Diagnostics.Debug.WriteLine` didn't. – Peter Mortensen Jun 14 '21 at 01:00
1

Try using:

Console.WriteLine()

The call to Debug.WriteLine will only be made during when DEBUG is defined.

Other suggestions are to use: Trace.WriteLine as well, but I haven't tried this.

There is also an option (not sure if Visual Studio 2008 has it), but you can still Use Debug.WriteLine when you run the test with Test With Debuggeroption in the IDE.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
VoodooChild
  • 9,776
  • 8
  • 66
  • 99
  • For later versions of [NUnit](https://en.wikipedia.org/wiki/NUnit): *"[Your Console.WriteLine(...) should appear in the output, but in NUnit 3, you should use TestContext.WriteLine(...)](https://stackoverflow.com/questions/46405753/how-do-i-get-nunit3-console-to-output-my-debug-to-screen-on-a-windows-box/46407376#46407376)"* – Peter Mortensen Jul 10 '23 at 09:39
1

Solved with the following example:

public void CheckConsoleOutput()
{
    Console.WriteLine("Hello, World!");
    Trace.WriteLine("Trace Trace the World");
    Debug.WriteLine("Debug Debug World");
    Assert.IsTrue(true);
}

After running this test, under 'Test Passed', there is the option to view the output, which will bring up the output window.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike
  • 620
  • 7
  • 21
1

For VS2022 in the Test Explorer the output will show up in the Test Detail Summary Pane. In additional you can right-click on your test and choose the 'Open test log' or simply Control+L and a testlog will open with all your Debug.WriteLine comments.

Further when .runsettings was attached to the unit test, please ensure that the CaptureTraceOutput was set to True.

<CaptureTraceOutput>True</CaptureTraceOutput>

DateTime myExpectedDateTime = sortedLPR[0].myDateTime;
DateTime myPreviousDateTime = sortedLPR[0].myDateTime;      // 1st date element
Debug.WriteLine($"(1) myPreviousDateTime: {myPreviousDateTime}");
Console.WriteLine($"(2) myPreviousDateTime: {myPreviousDateTime}");

testlog

Please notice the order of the output: (2) before (1) - standard output before debug output.

0

Are you sure you're running your unit tests in Debug? Debug.WriteLine won't be called in Release builds.

Two options to try are:

  • Trace.WriteLine(), which is built into release builds as well as debug

  • Undefine DEBUG in your build settings for the unit test

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jason Williams
  • 56,972
  • 11
  • 108
  • 137
0

Console.WriteLine won't work. Only Debug.WriteLine() or Trace.WriteLine() will work, in debug mode.

I do the following: include using System.Diagnostics in the test module. Then, use Debug.WriteLine for my output, right click on the test, and choose Debug Selected Tests. The result output will now appear in the Output window below. I use Visual Studio 2017 version 15.8.1, with the default unit test framework Visual Studio provides.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Goodies
  • 1,951
  • 21
  • 26
0

A different variant of the cause/solution:

My issue was that I was not getting an output because I was writing the result set from an asynchronous LINQ call to the console in a loop in an asynchronous context:

var p = _context.Payment.Where(pp => pp.applicationNumber.Trim() == "12345");

p.ForEachAsync(payment => Console.WriteLine(payment.Amount));

And so the test was not writing to the console before the console object was cleaned up by the runtime (when running only one test).

The solution was to convert the result set to a list first, so I could use the non-asynchronous version of forEach():

var p = _context.Payment.Where(pp => pp.applicationNumber.Trim() == "12345").ToList();

p.ForEachAsync(payment =>Console.WriteLine(payment.Amount));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Richard Strickland
  • 1,771
  • 17
  • 8
-1

Trace.WriteLine should work provided you select the correct output (the dropdown labeled with "Show output from" found in the Output window).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131