How can I write output from a unit test?

后端 未结 15 986
误落风尘
误落风尘 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: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.

提交回复
热议问题