【问题标题】:Formatting Exception displayed when a unit test fails (xunit , C#)单元测试失败时显示格式异常(xunit,C#)
【发布时间】:2022-01-04 17:55:57
【问题描述】:

我正在使用 .Net6 + Xunit + Fluent Assertions 并尝试格式化遇到异常时输出到控制台的异常显示。 (我真正想看到的是Exception 上的Data 属性)

在我的程序本身中,我向AppDomain.UnhandledException 添加了一个处理程序,但在测试中,这根本不会改变输出。

我一直在寻找一种为异常设置自定义格式化程序的替代方法,但在 Fluent Assertions 和 Xunit 中都没有找到方法。

有没有办法在测试中格式化异常输出?

【问题讨论】:

  • 我认为你可以用action.Should().Throw<YourCustomException>().Where(e => e.Data.GetValueOrDefault("YourKey") == "expected value");断言异常的属性

标签: c# xunit vstest fluent-assertions


【解决方案1】:

在您的测试类中,在构造函数中

    private readonly ITestOutputHelper _outputHelper;

    public TestCaseClass(ITestOutputHelper outputHelper)
    {
        _outputHelper = outputHelper;
    }

然后(我不建议这样做)将您的行为和断言部分代码包装在 try/catch 中。捕获异常,使用输出助手记录数据字段,然后重新抛出。

try 
{ 
    // Act

   // Assert
} 
catch(Exception ex) 
{ 
    _outputHelper.WriteLine(ex.Data)
    throw;
}

这不是被问到的,但您应该考虑登录您的实际代码,您可以将大多数日志框架连接到 xUnit 的输出助手,并让您的应用程序非常愉快地登录到测试控制台。

【讨论】:

    【解决方案2】:

    我通过库 XunitContext (nuget) 找到了一种方法

    示例代码:

    public static class GlobalSetup {
        [ System.Runtime.CompilerServices.ModuleInitializer ]
        public static void Setup( ) {
            XunitContext.EnableExceptionCapture();
        }
    }
    
    public class TestExceptionSample :
        XunitContextBase {
        [ Fact ]
        public void TestExceptionUsage( ) {
            // This test will fail
            Assert.False( true );
        }
    
        [ Fact ]
        public void TestExceptionExceptionUsage( ) {
            // Exception is thrown
            Exception exception = new Exception( "MY EXCEPTION" );
            exception.Data.Add( "Key1", "Value1" );
            throw exception;
        }
    
        public TestExceptionSample( ITestOutputHelper output ) :
            base( output ) { }
    
        public override void Dispose( ) {
            var theExceptionThrownByTest = Context.TestException;
            var testDisplayName          = Context.Test.DisplayName;
            var testCase                 = Context.Test.TestCase;
            Output.WriteLine( "Handling Exception" );
            if ( Context.TestException?.Data.Count > 0 ) {
                foreach ( var key in Context.TestException.Data.Keys ) {
                    Output.WriteLine( $"{key} => {Context.TestException.Data[ key ]}" );
                }
            }
        }
    }
    

    输出:

     Failed Tests.TestExceptionSample.TestExceptionUsage [4 ms]
      Error Message:
       Assert.False() Failure
    Expected: False
    Actual:   True
      Stack Trace:
         at Tests.TestExceptionSample.TestExceptionUsage() in /Tests.cs:line 108
      Standard Output Messages:
     Handling Exception
    
    
      Failed Tests.TestExceptionSample.TestExceptionExceptionUsage [< 1 ms]
      Error Message:
       System.Exception : MY EXCEPTION
      Stack Trace:
         at Tests.TestExceptionSample.TestExceptionExceptionUsage() in /Tests.cs:line 115
      Standard Output Messages:
     Handling Exception
     Key1 => Value1
    
    
    
    Test Run Failed.
    Total tests: 2
         Failed: 2
     Total time: 1.4345 Seconds
    

    【讨论】:

      猜你喜欢
      • 2016-09-20
      • 2016-06-22
      • 2021-04-12
      • 2013-08-12
      • 2014-04-09
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      • 2019-04-09
      相关资源
      最近更新 更多