2012-02-02 33 views
1

我正在使用帶有Gallio/MbUnit的PostSharp。如何在Gallio/MbUnit引發異常時編寫自定義消息?

我希望能夠從測試用例中捕獲異常,併爲捕獲異常的異常寫入自定義消息。例外情況會被PostSharp方面所捕獲。例如,我想一些功能WriteToExceptionLog這樣

[Serializable] 
public class MyAspect: OnExceptionAspect 
{ 
    public override void OnException(MethodExecutionArgs args) 
    { 
     WriteToExceptionLog("My custom message"); 
    } 
} 

會趕上

[TestFixture] 
public class MyClass 
{ 
    [Test] 
    public void MyTest() 
    { 
     throw new NotImplementedException(); 
    } 
} 

和日誌會顯示「我的自定義消息」,而不是一個NotImplementedException。

我該怎麼做?到哪裏寫入異常消息?

+0

什麼導致你相信例外被寫入任何日誌? – 2012-02-02 18:42:20

+0

Gallio或TestDriven.NET中讀取異常消息的是什麼?從他們讀什麼? – cm007 2012-02-02 18:46:08

+0

我不知道這些產品,但NUnit和MSTest不「讀」任何東西。他們正在調用測試,所以例外只是傳播給他們。 – 2012-02-02 18:47:24

回答

1

你不需要請使用P#以實現:

public class ExceptionInterceptorAttribute : TestAttribute 
{ 
    protected override void Execute(PatternTestInstanceState state) 
    { 
     try 
     { 
      base.Execute(state); 
     } 
     catch (Exception e) 
     { 
      TestLog.Failures.WriteLine("Intercepted an exception of type: {0}", e.GetType()); 
     } 
    } 
} 

public class ExceptionInterceptorTests 
{ 
    [ExceptionInterceptor] 
    public void With_interceptor() 
    { 
     throw new NotImplementedException(); 
    } 

    [Test] 
    public void Without_interceptor() 
    { 
     throw new NotImplementedException(); 
    } 
} 
相關問題