locked
Unit tests + Console.Out ? RRS feed

  • Question

  • Hi Folks,

       this is an extreamly simple question (I'm nearly too embarassed to ask it), but I can't seem to get Console.Out working for a simple Unit Test. The code compiles/runs, but nothing is printing in the OUTPUT windows or any of the windows i can see.

     

    What I did was :-

    1. Make a new TEST project.

    2. Goto the sample UnitTest1.cs file (or whatever the template creates) and in the sample TestMethod created, i added the following the two lines only.

    Console.Out.WriteLine("Testing Console.Out in a Unit Test.");

    Console.Out.Flush();

     

    Compiled, Ran (No debug .. and later in debug mode) and i couldn't find the output.

     

    So - does Console.Out work for Unit Tests?

     

    Ok - why? I'm trying to see my LINQ code by setting the linq's DataContext instance (i've created) Log property to the Console.Out

     

    eg. myLinqDataContext.Log = Console.Out

     

    code compiles but I can't find the output Sad

     

    What simple thing have i done wrong?

     

    Cheers!

     

    -PK-

    Thursday, December 27, 2007 12:47 PM

Answers

  • Hello,

     

    Console output should definitely be contained in the unit test result.  Once your test has completed, double click on the row for this test in the "Test Results" window; this will open up a new window that contains detailed information about the results of your test.  There should be a "Common Results" area containing information such as the name of the test, the outcome, the time at which it was executed, the duration of the test, etc.  If your test wrote to the console, there should also be an area titled "Standard Console Output" that displays this text.

     

    If this does not solve the problem, please verify that the test is actually executing your call to Console.Out.WriteLine.  If, for example, this call occurs after the test has failed, it will never be executed.  This could easily happen if you appended your code to the end of a generated test method, which by default calls Assert.Inconclusive and fails the test.

     

    If this does not help, please provide the source for test test method that exhibits this behavior, as well as information about the version of Visual Studio you are using.

     

    Thanks,

     

    Neal Fowler

    Visual Studio Team Test

    Monday, December 31, 2007 7:19 PM

All replies

  • Hello,

     

    Console output should definitely be contained in the unit test result.  Once your test has completed, double click on the row for this test in the "Test Results" window; this will open up a new window that contains detailed information about the results of your test.  There should be a "Common Results" area containing information such as the name of the test, the outcome, the time at which it was executed, the duration of the test, etc.  If your test wrote to the console, there should also be an area titled "Standard Console Output" that displays this text.

     

    If this does not solve the problem, please verify that the test is actually executing your call to Console.Out.WriteLine.  If, for example, this call occurs after the test has failed, it will never be executed.  This could easily happen if you appended your code to the end of a generated test method, which by default calls Assert.Inconclusive and fails the test.

     

    If this does not help, please provide the source for test test method that exhibits this behavior, as well as information about the version of Visual Studio you are using.

     

    Thanks,

     

    Neal Fowler

    Visual Studio Team Test

    Monday, December 31, 2007 7:19 PM
  • Thank you,

     

    that works fine. Just one little request. If you can make the Test Result window as a separate window so if I run one test two times I do not need to close the last result window and double click on the test again in order to see the new one. This is how the resharper debugger worked and it was quite useful. Another way to do that would be another window just for Console Output.

     

    Thank you in advance

    Vlad

    Tuesday, January 8, 2008 6:05 AM
  • Continuing this line of thought... I'm testing code the uses Linq-to-Sql and I have set the Log property of the DataContext to Console.Out - but the output is not captured by the test runner, there's nothing on the result other than the Common Results.

    Method being called contains this:

    DataContext.Log = Console.Out;

    Thanks.


    Toby
    Wednesday, January 7, 2009 7:25 PM
  • One workaround for this is to use the DebuggerWriter class from here -> http://www.u2u.info/Blogs/Kris/Lists/Posts/Post.aspx?ID=11

    This will output the log to the VS debugger output window which is better than nothing.
    Toby
    Wednesday, January 7, 2009 8:01 PM
  • The "System.Diagnostics.Debug.WriteLine" in place of "Console.Out.WriteLine" is working for me.

    CBD
    Wednesday, August 12, 2009 3:54 PM
  •  "System.Diagnostics.Debug.WriteLine" worked for me too.. it writes to the Output Window.. i actually used Debug.Write("blah"), which is in the System.Diagnostics namespace..

    Debug >> Windows >> Output

    Select "Debug" from the "Show output from" drop down to see output text

    Thanks!!!


    • Edited by BinaryFuel Thursday, October 20, 2011 5:51 PM clarification
    • Proposed as answer by lampiclobe Tuesday, November 13, 2012 9:42 PM
    • Unproposed as answer by lampiclobe Tuesday, November 13, 2012 9:43 PM
    Thursday, October 20, 2011 5:48 PM
  • You can check this approach - Getting console output within a unit test .

    Hope this helps!


    Monday, November 19, 2012 9:15 AM