xUnit.net does not capture console output

前端 未结 5 1346
轮回少年
轮回少年 2020-12-05 06:39

I just started testing xUnit.net, but it doesn\'t seem to capture any output (Console, Debug, Trace), as I would have expected.

Is that possible? I am using a sample

5条回答
  •  自闭症患者
    2020-12-05 07:27

    I landed here with the same question. Here's what I ended up with. I hope it helps someone else.

    How to write a custom target

        /// 
        ///     Use this to output NLog information when running test cases.
        /// 
        [Target("XUnit")]
        public class XUnitTarget : TargetWithLayout
        {
            [RequiredParameter] public ITestOutputHelper OutputHelper { get; set; }
    
            protected override void Write(LogEventInfo logEvent)
            {
                var logMessage = Layout.Render(logEvent);
                OutputHelper.WriteLine(logMessage);
            }
    
            /// 
            /// Call this in the test where you wish to enable logging.
            /// 
            /// The xUnit output helper from the test.
            public static void Configure(ITestOutputHelper testOutputHelper)
            {
                var config = new LoggingConfiguration();
                var xUnitTarget = new XUnitTarget
                {
                    OutputHelper = testOutputHelper
                };
                config.AddTarget("xUnit", xUnitTarget);
                config.AddRule(LogLevel.Trace, LogLevel.Fatal, xUnitTarget);
                LogManager.Configuration = config;
            }
        }
    

提交回复
热议问题