2012-02-02 13 views
1

私はGallio/MbUnitでPostSharpを使用しています。Gallio/MbUnitのように例外がスローされたときにカスタムメッセージを書き込む方法は?

私はテストケースから、例外をキャッチし、例外が書き込まれたどこにでもキャッチする例外のカスタムメッセージを書くことができるようにしたいと考えています。例外はポストシャープの側面で捕捉されます。例えば、私はいくつかの機能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

例外が任意のログに書き込まれていることを信じるようにあなたを導き何? –

+0

GallioまたはTestDriven.NETで例外メッセージを読み取るのは何ですか?彼らは何を読むのでしょうか? – cm007

+0

私はそれらの製品を知らないが、NUnitとMSTestは何も "読んでいない"。彼らはテストを呼び出しているので、例外は単にそれらに伝播します。 –

答えて

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(); 
    } 
} 
関連する問題