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

前端 未结 12 1066
长情又很酷
长情又很酷 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:26

    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.

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

    Debug.WriteLine() can be used as well.

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

    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.

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

    Visual Studio For Mac

    None of the other solutions worked on Visual Studio for Mac

    If you are using NUnit, you can add a small .NET Console Project to your solution, and then reference the project you wish to test in the References of that new Console Project.

    Whatever you were doing in your [Test()] methods can be done in the Main of the console application in this fashion:

    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Console");
    
            // Reproduce the unit test
            var classToTest = new ClassToTest();
            var expected = 42;
            var actual = classToTest.MeaningOfLife();
            Console.WriteLine($"Pass: {expected.Equals(actual)}, expected={expected}, actual={actual}");
        }
    }
    

    You are free to use Console.Write and Console.WriteLine in your code under these circumstances.

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

    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.

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

    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.

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