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

前端 未结 12 1065
长情又很酷
长情又很酷 2020-12-04 13:32

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.W         


        
相关标签:
12条回答
  • 2020-12-04 14:12

    First of all unit tests are, by design, supposed to run completely without interaction.

    With that aside, I don't think there's a possibility that was thought of.

    You could try hacking with the AllocConsole P/Invoke which will open a console even when your current application is a GUI application. The Console class will then post to the now opened console.

    0 讨论(0)
  • 2020-12-04 14:12

    I have an easier solution (that I used myself recently, for a host of lazy reasons). Add this method to the class you are working in:

    public static void DumbDebug(string message)
    {
        File.WriteAllText(@"C:\AdHocConsole\" + message + ".txt", "this is really dumb. I wish Microsoft had more obvious solutions to its solutions problems.");
    }
    

    Then...open up the directory AdHocConsole, and order by created time. Make sure when you add your 'print statements'. They are distinct though, else there will be juggling.

    0 讨论(0)
  • 2020-12-04 14:13

    In Visual Studio 2017, "TestContext" doesn't show the Output link into Test Explorer.

    However, Trace.Writeline() shows the Output link.

    0 讨论(0)
  • 2020-12-04 14:18

    You can use

    Trace.WriteLine()
    

    to write to the Output window when debugging a unit test.

    0 讨论(0)
  • 2020-12-04 14:18

    IMHO, output messages are relevant only for failed test cases in most cases. I made up the below format, and you can make your own too. This is displayed in the Visual Studio Test Explorer Window itself.

    How can we throw this message in the Visual Studio Test Explorer Window?

    Sample code like this should work:

    if(test_condition_fails)
        Assert.Fail(@"Test Type: Positive/Negative.
                    Mock Properties: someclass.propertyOne: True
                    someclass.propertyTwo: True
                    Test Properties: someclass.testPropertyOne: True
                    someclass.testPropertyOne: False
                    Reason for Failure: The Mail was not sent on Success Task completion.");
    

    You can have a separate class dedicated to this for you.

    0 讨论(0)
  • 2020-12-04 14:22

    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.

    0 讨论(0)
提交回复
热议问题