2016-08-05 4 views
0

私はいくつかのテストケースにKnownFaultを注釈したいと思っています。これはexpectExceptionの機能にYouTrackのREST APIを使った魔法を加えたものです。 IntermittentFailure属性を持っていて、[例外] [メッセージ]でテストが失敗することがあることに気付いていますが、残りのビルドチェーンをブロックしたくないということを意味します。testng - KnownFaultとIntermittentFailureの注釈を作成する

した後、私は私のテストクラスは、IHookableを実装する必要があり、私はこのような何か持っていることがわかっいくつかの研究:

final Throwable[] error = new Throwable[1]; 

IHookCallBack callback = new IHookCallBack() { 
    @Override 
    public void runTestMethod(ITestResult tr) { 
    try { 
     invokeMethod(thisMethod, testInstance, parameters); 
    } catch (Throwable t) { 
     error[0] = t; 
     tr.setThrowable(t); // make Throwable available to IHookable 
    } 
    } 

    @Override 
    public Object[] getParameters() { 
    return parameters; 
    } 
}; 
hookable.run(callback, testResult); 
if (error[0] != null) { 
    throw error[0]; 
} 

@Override 
public void run(IHookCallBack callBack, ITestResult result) { 
    callBack.runTestMethod(result); 
    if (result.getThrowable().getCause() instanceof IllegalArgumentException){ 
     System.out.println("This is expected."); 
     result.setThrowable(null); 
    } 
    else{ 
     System.out.println("Unexpected exception"); 
    } 
} 

これに伴う問題は、invokeHookableの実際の実装があるが残念なことに、最後の行は、error配列がrunメソッドで完全に私の手に届かなくても、私のテストケースが例外をスローすることを意味します。

例外を傍受し、それを私がしたいと思うように適切に処理する方法は何でしょうか?

答えて

1

あなたがしようとしていることは本当に面白いです。あなたは変更を提案しようとする必要がありますhttps://github.com/cbeust/testng/pull/

しかし、おそらくIHookableあなたが使用できる最高のリスナーではありません。 IInvokedMethodListenerを試しましたか?

void afterInvocation(IInvokedMethod method, ITestResult result) { 
    if (result.getThrowable().getCause() instanceof IllegalArgumentException) { 
     System.out.println("This is expected."); 
     result.setThrowable(null); 
     result.setStatus(SUCCESS); // If you want to change the status 
    } else { 
     System.out.println("Unexpected exception"); 
    } 
} 
+0

涼しい。したがって、リスナーを使用する場合は、基本クラスを少し変更する必要があります。提案する実装​​を行う 'TestListener'というクラスを追加した後、テストの基本クラスに' @Listeners(TestListener.class) 'アノテーションを付ける必要があります。 。チャームのように働いた! – RekaB

+0

http://testng.org/doc/documentation-main.html#testng-listenersが好きな場合は、他のリスナー検出メカニズムを使用できます – juherr

関連する問題