Using ReSharper, how to show debug output during a long-running unit test?

后端 未结 7 1074
误落风尘
误落风尘 2020-12-28 13:06

I\'m using xUnit with the ReSharper test runner and the xUnitContrib resharper plugin.

When I have a long-running test, I\'d like to be able to output some progress

相关标签:
7条回答
  • 2020-12-28 13:34

    For NUnit this works:

    Console.SetOut(TestContext.Progress);
    

    ** The late answer is because I had the same problem, and I just solved it. may help others

    0 讨论(0)
  • 2020-12-28 13:39

    ReSharper somehow removed the default listener in unit tests. To display text in the Output window, just add this line:

    Debug.Listeners.Add(new DefaultTraceListener());
    
    0 讨论(0)
  • 2020-12-28 13:39

    You may also consider using xunit.NLog which is a very nice and clean way to inject ITestOutputhelper into your SUTs.

    http://www.tomdupont.net/2015/06/capture-xunit-test-output-with-nlog-and.html

    0 讨论(0)
  • 2020-12-28 13:44

    Per Brad Wilson:

    This is a limitation in xUnit.net, not the Resharper adapter.

    We will be addressing this in v2 of xUnit.net.

    http://xunitcontrib.codeplex.com/workitem/4160

    0 讨论(0)
  • 2020-12-28 13:53

    If you used xUnit.net 1.x, you may have previously been writing output to Console, Debug, or Trace. When xUnit.net v2 shipped with parallelization turned on by default, this output capture mechanism was no longer appropriate; it is impossible to know which of the many tests that could be running in parallel were responsible for writing to those shared resources. Users who are porting code from v1.x to v2.x should use one of the two new methods instead.

    Have a look here for example of how to perform logging with xUnit.net v2:

    http://xunit.github.io/docs/capturing-output.html

    This is the example:

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-28 13:55

    XunitLogger uses AsyncLocal<T> to keep track of the logging context so calls to Trace.Writeline and Console.Writeline can be routed to the correct instance of ITestOutputHelper.

    Usage:

    static class ClassBeingTested
    {
        public static void Method()
        {
            Trace.WriteLine("From Trace");
            Console.WriteLine("From Console");
            Console.Error.WriteLine("From Console Error");
        }
    }
    
    public class TestBaseSample  :
        XunitLoggingBase
    {
        [Fact]
        public void Write_lines()
        {
            WriteLine("From Test");
            ClassBeingTested.Method();
    
            var logs = XunitLogger.Logs;
    
            Assert.Contains("From Test", logs);
            Assert.Contains("From Trace", logs);
            Assert.Contains("From Console", logs);
            Assert.Contains("From Console Error", logs);
        }
    
        public TestBaseSample(ITestOutputHelper output) :
            base(output)
        {
        }
    }
    
    0 讨论(0)
提交回复
热议问题