Can I write into the console in a unit test? If yes, why doesn't the console window open?

204,293

Solution 1

NOTE: The original answer below should work for any version of Visual Studio up through Visual Studio 2012. Visual Studio 2013 does not appear to have a Test Results window any more. Instead, if you need test-specific output you can use @Stretch's suggestion of Trace.Write() to write output to the Output window.


The Console.Write method does not write to the "console" -- it writes to whatever is hooked up to the standard output handle for the running process. Similarly, Console.Read reads input from whatever is hooked up to the standard input.

When you run a unit test through Visual Studio 2010, standard output is redirected by the test harness and stored as part of the test output. You can see this by right-clicking the Test Results window and adding the column named "Output (StdOut)" to the display. This will show anything that was written to standard output.

You could manually open a console window, using P/Invoke as sinni800 says. From reading the AllocConsole documentation, it appears that the function will reset stdin and stdout handles to point to the new console window. (I'm not 100% sure about that; it seems kind of wrong to me if I've already redirected stdout for Windows to steal it from me, but I haven't tried.)

In general, though, I think it's a bad idea; if all you want to use the console for is to dump more information about your unit test, the output is there for you. Keep using Console.WriteLine the way you are, and check the output results in the Test Results window when it's done.

Solution 2

Someone commented about this apparently new functionality in Visual Studio 2013. I wasn't sure what he meant at first, but now that I do, I think it deserves its own answer.

We can use Console.WriteLine normally and the output is displayed, just not in the Output window, but in a new window after we click "Output" in the test details.

Enter image description here

Solution 3

You could use this line to write to Output Window of the Visual Studio:

System.Diagnostics.Debug.WriteLine("Matrix has you...");

Must run in Debug mode.

Solution 4

As stated, unit tests are designed to run without interaction.

However, you can debug unit tests, just like any other code. The easiest way is to use the Debug button in the Test Results tab.

Being able to debug means being able to use breakpoints. Being able to use breakpoints, then, means being able to use Tracepoints, which I find extremely useful in every day debugging.

Essentially, Tracepoints allow you to write to the Output window (or, more accurately, to standard output). Optionally, you can continue to run, or you can stop like a regular breakpoint. This gives you the "functionality" you are asking for, without the need to rebuild your code, or fill it up with debug information.

Simply add a breakpoint, and then right-click on that breakpoint. Select the "When Hit..." option:

When hitting option

Which brings up the dialog:

When a breakpoint is hit

A few things to note:

  1. Notice that the breakpoint is now shown as a diamond, instead of a sphere, indicating a trace point
  2. You can output the value of a variable by enclosing it like {this}.
  3. Uncheck the "Continue Execution" checkbox to have the code break on this line, like any regular breakpoint
  4. You have the option of running a macro. Please be careful - you may cause harmful side effects.

See the documentation for more details.

Solution 5

There are several ways to write output from a Visual Studio unit test in C#:

  • Console.Write - The Visual Studio test harness will capture this and show it when you select the test in the Test Explorer and click the Output link. Does not show up in the Visual Studio Output Window when either running or debugging a unit test (arguably this is a bug).
  • Debug.Write - The Visual Studio test harness will capture this and show it in the test output. Does appear in the Visual Studio Output Window when debugging a unit test, unless Visual Studio Debugging options are configured to redirect Output to the Immediate Window. Nothing will appear in the Output (or Immediate) Window if you simply run the test without debugging. By default only available in a Debug build (that is, when DEBUG constant is defined).
  • Trace.Write - The Visual Studio test harness will capture this and show it in the test output. Does appear in the Visual Studio Output (or Immediate) Window when debugging a unit test (but not when simply running the test without debugging). By default available in both Debug and Release builds (that is, when TRACE constant is defined).

Confirmed in Visual Studio 2013 Professional.

Share:
204,293

Related videos on Youtube

vel
Author by

vel

Updated on July 23, 2022

Comments

  • vel
    vel almost 2 years

    I have a test project in Visual Studio. I use Microsoft.VisualStudio.TestTools.UnitTesting.

    I add this line in one of my unit tests:

    Console.WriteLine("Some foo was very angry with boo");
    Console.ReadLine();
    

    When I run the test, the test passes, but the console window is not opened at all.

    Is there a way to make the console window available to be interacted via a unit test?

    • seldary
      seldary almost 12 years
      It really depends on the runner. You can use TestDriven.Net (a great, free for personal use, test runner) - Console.WriteLine will write to VS output pane.
    • GrayFox374
      GrayFox374 almost 12 years
      Thanks for spreading the word on TestDriven.Net
    • DharmaTurtle
      DharmaTurtle about 4 years
      NCrunch also has this feature, which alone makes it worth the price IMO. I have a Dump extension method that outputs the object's contents into the console, making things so much easier to debug. i.imgur.com/MEZwy7X.png
    • Admin
      Admin over 3 years
      In general, there is no requirement to print string in unit test. But, if you need, there is another link to perfectly solve your problem How can I write output from a unit test?. Your should open test console, rather than normal console.
  • vel
    vel almost 12 years
    Hmm.. My main motive is to write some extra data to console to see some deeper details. It will be adhoc thing which I may not need later.
  • sinni800
    sinni800 almost 12 years
    Try opening a new WindowsApplication, using AllocConsole to allocate a console and it will write there. I don't know what it really does but it might not work in a Unit Test environment. It really would be nice to know...
  • Michael Edenfield
    Michael Edenfield almost 12 years
    hrm. re-reading AllocConsole documentation, I may be incorrect, but I'd have to test it.
  • Robert Patterson
    Robert Patterson over 11 years
    For me this comment from Michael said everything I needed to think about: "When you run a unit test through VS2010, standard output is redirected by the test harness and stored as part of the test output."
  • Dmitry Pavlov
    Dmitry Pavlov over 11 years
    check this thread for the details - stackoverflow.com/questions/1159755/…
  • Bkwdesign
    Bkwdesign over 10 years
    I second @Robert Patterson. This was the reminder I was looking for. Now that I can find my output as part of the test details, I'm happy..
  • Stretch
    Stretch about 9 years
    VS2013 - Test Results window is no longer present, so don't look for it. Instead, you can use Trace.Write(), and the output will be in the Output window
  • Visar
    Visar over 8 years
    in VS2013, if your write on the Console, there will be an [Output] link label on the TestExplorer/Test/Summary. Click on it and you get the desired output. Wait? You want also to call Console.ReadLine()??
  • Isaac Baker
    Isaac Baker about 8 years
    This worked great for me using MSTest and R# to view the output. Thanks!
  • yoyo
    yoyo about 8 years
    Note that the rules are slightly different if you use NUnit and the NUnit Test Adapter extension for Visual Studio rather than the built-in Microsoft test framework.
  • garfbradaz
    garfbradaz about 7 years
    Excellent solution. Confirm it works in VS2015 Enterprise.
  • O. R. Mapper
    O. R. Mapper about 7 years
    Unfortunately, one cannot select any text in that output area for copy-and-pasting :( What were they thinking?!
  • bychance
    bychance almost 7 years
    @O.R.Mapper, right click on "Standard Output" area, and choose copy all
  • ovi
    ovi almost 7 years
    I am using vs2013 community edition, the "output" link in the test details doesn't show up :( is there a setting somewhere that brings it up?
  • Tiago Duarte
    Tiago Duarte almost 7 years
    @Ovi-WanKenobi try outputting something with Console.Write
  • ovi
    ovi almost 7 years
    yeah, that "output" link doesn't show up unless you output something first... thank you.
  • Vinod Srivastav
    Vinod Srivastav over 6 years
    I guess you need to write Debug.Print("") to achieve this Console.WriteLine doesn't make sense
  • Pratyush Dhanuka
    Pratyush Dhanuka over 6 years
    thnx for images, was stuck for hours and could not find the output page link
  • kiml42
    kiml42 about 6 years
    This looks really useful, but I don't see the button in VS 2017.
  • William
    William about 6 years
    Trace.WriteLine("....") only appears in the output window if you debug the unit test
  • heringer
    heringer about 6 years
    In V2017 (Community), go to "test explorer", select the test result item in the list, then click on the link "output" on the other window (test result window?). Since it will probably be truncated, use the "copy all" and past somewhere else.
  • sajad
    sajad almost 6 years
    Just a note, if you need to see the complete output, right click and copy the output.
  • inliner49er
    inliner49er almost 6 years
    In vs 2017 you have to select EACH test that's run, then click output - not very useful when you have a lot of tests. I want to see all of the output together - not in separate windows.
  • Mohamed Ali
    Mohamed Ali almost 6 years
    or Debug.Print()
  • I Stand With Israel
    I Stand With Israel over 5 years
    @inliner49er I'm still not seeing the "Output" link in VS17, even when running each test individually. I asked this question separately and it appears as if that's a feature granted by ReSharper. Can you confirm this?
  • Pete Kirkham
    Pete Kirkham over 5 years
    Use of console output will cause the test run to abort, apparently at random, when it runs out of memory.
  • yoyo
    yoyo over 5 years
    @William it shows up if you debug a test, but not if you simply run it without debugging.
  • Douglas Gaskell
    Douglas Gaskell almost 5 years
    Not seeing output in 2019.
  • TroySteven
    TroySteven over 4 years
    Just an FYI, its Trace.WriteLine
  • TroySteven
    TroySteven over 4 years
    I fiqured I'd add this in as well. In Visual Studio 2019 using XUNIT you can do the following: Assert.True(success, "DELETE RECORD FAILED"); DELETE RECORD FAILED will then show up in VS 2019 Test Explorer. Again, like the answer above, this only works when your unit test has failed the expected condition.
  • Clyde D'Souza
    Clyde D'Souza about 4 years
    Excellent. Works in VS 2019 Professional as well.
  • Tolga
    Tolga about 4 years
    In VS2019, go to "Test Explorer", select the test in question and on the bottom "Test Detail Summary" panel, click the "Open additional output for this result" link. (You can right click anywhere in the Standard Output window to Copy All)