How can I write output from a unit test?

后端 未结 15 959
误落风尘
误落风尘 2020-11-28 20:56

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 print

相关标签:
15条回答
  • 2020-11-28 21:57

    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.

    0 讨论(0)
  • 2020-11-28 21:58

    In Visual Studio 2017, you can see the output from test explorer.

    1) In your test method, Console.WriteLine("something");

    2) Run the test.

    3) In Test Explorer window, click the Passed Test Method.

    4) And click the "Output" link.

    And click "Output", you can see the result of Console.Writeline().

    0 讨论(0)
  • 2020-11-28 21:59

    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.

    using Xunit;
    using Xunit.Abstractions;
    
    public class MyTestClass
    {
        private readonly ITestOutputHelper output;
    
        public MyTestClass(ITestOutputHelper output)
        {
            this.output = output;
        }
    
        [Fact]
        public void MyTest()
        {
            var temp = "my class!";
            output.WriteLine("This is output from {0}", temp);
        }
    }
    

    There's another method listed in the link I provided for writing to your Output window, but I prefer the previous.

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