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.
Try using
TestContext.WriteLine()
which outputs text in test results.Example:
The "magic" is described in MSDN as "The test framework automatically sets the property, which you can then use in unit tests."
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 VS2008 has it) but you can still Use
Debug.WriteLine
when you run the test withTest With Debugger
option in the IDEIts a little late to the conversation.
I was also trying to get Debug or Trace or Console or TestContext to work in Unit Testing.
None of these methods would appeared work or show output in the output window.
VS2012 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 VS2012
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.
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.github.io/docs/capturing-output.html
This method groups your output with each specific unit test.
There's another method listed in the link I provided for writing to your Output window, but I prefer the previous.
Just ran into this. The cause/solution is a different variant of the above so I'll post.
My issue was that I was not getting an output because I was writing the result set from an async Linq call to console in a loop in an async context:
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). Solution was to convert the result set to a list first so i could use the non async version of forEach():
I get no output when my Test/Test Settings/Default Processor Architecture setting and the assemblies that my test project references are not the same. Otherwise Trace.Writeline() works fine.