2012-02-02 8 views
1

Gallio/MbUnit에서 PostSharp를 사용하고 있습니다.Gallio/MbUnit처럼 예외가 발생하면 맞춤 메시지를 작성하는 방법은 무엇입니까?

테스트 케이스에서 예외를 catch하고 예외가 기록 된 곳이면 어디든 catch하는 예외에 대한 사용자 지정 메시지를 작성할 수 있기를 바랍니다. 예외는 PostSharp 측면에서 발견 할 수 있습니다. 예를 들어, 나는

[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을 보여줄 것 등 몇 가지 기능 WriteToExceptionLog을하고 싶습니다.

어떻게하면됩니까? 예외 메시지는 어디에 쓰여 집니까?

+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(); 
    } 
}