如何从MbUnit TestContext检索导致测试失败的异常

时间:2012-03-12 00:59:14

标签: unit-testing mbunit

我看过TestContext.CurrentContext.Outcome,但它没有我想要的东西。

由于

的Pawel

2 个答案:

答案 0 :(得分:2)

虽然可以从日志中提取堆栈跟踪等,但未存储异常对象(请参阅https://github.com/Gallio/Gallio-VS2011-Integration/blob/master/MbUnitAdapter/MbUnitAdapter/StackTraceHunter.cs)。

可能最好的事情是子类TestAttribute:

public class InspectExceptionAttribute : TestAttribute
{
    protected override void Execute(PatternTestInstanceState state)
    {
        try
        {
            base.Execute(state);
        }
        catch (Exception e)
        {
            // do something with e
        }
    }
}

public class InspectExceptionTests
{
    [InspectException]
    public void With_interceptor()
    {
        throw new NotImplementedException();
    }

    [Test]
    public void Without_interceptor()
    {
        throw new NotImplementedException();
    }
}

答案 1 :(得分:0)

如果我正确理解你的问题,我认为你可以使用try-catch语句捕获Exception然后从那里使用它