Step 1: Open Visual Studio 2013. Step 2: Create a new project in Visual Studio 2013. Seelct "FILE" -> "New" -> "Project...". Step 3: Select the project language as C# and select a Console Application and enter the name of the project as Mathematics (Project Name).
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.
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.
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.
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:
Which brings up the dialog:
A few things to note:
See the documentation for more details.
There are several ways to write output from a Visual Studio unit test in C#:
Confirmed in Visual Studio 2013 Professional.
You can use
Trace.WriteLine()
to write to the Output window when debugging a unit test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With