2017-01-25 4 views
0

2つのTestNGリスナーがログ情報をログファイルに報告し、デバッグ情報を取得しています。これらはIConfigurationListener2ITestListenerです。テストは複数のスレッドで実行されます。TestNG:IConfigurationListener2.beforeConfiguration ITestResultとITestListener.onTestStartをリンクする方法

私の問題は、私は@TestITestResult.getMethodName()を取得するためにITestListener.onTestStart()ITestResultIConfigurationListener2.onConfigurationFailure()方法でITestResultをリンクする必要があるということです。これは可能ですか?

TestListenerコードは次のとおりです。

public class TestListener implements ITestListener{ 
    @Override 
    public void onTestStart(ITestResult result) {  
     System.out.println("Starting test method:" + result.getMethod().getMethodName()); 
    } 
} 

IConfigurationListener2は次のとおりです。

public class ConfigurationListener implements IConfigurationListener2 { 
    @Override 
    public void onConfigurationFailure(ITestResult result) { 
     System.out.println("Failed to run " + result.getMethod().getMethodName() 
      + " for the test method:" + <code needed>); 
} 

TestNGクラスは次のとおりです。

public class Script1 { 

private int i =0; 
@BeforeMethod 
public void before(){ 
    System.out.println("Starting before"); 
    i++; 
    if (i==1){ 
     throw new RuntimeException("forcing an exception"); 
    } 
} 

@Test(testName="script1") 
public void script1_run(){ 
    System.out.println("Running script"); 
} 

@Test(testName="script2") 
public void script2_run(){ 
    System.out.println("Running script"); 
} 
} 

それでは、どのように私はどの@Test方法@beforeMethod FAを見つけるのですか〜のために育てられた。 、script2_run

感謝を:
は、テストメソッドのために前に実行することができませんでした前に開始スクリプト
を実行script1_run

は、試験方法を開始
前に開始:私はこのようなログに何かをしたいと思います

+0

後にスキップされることになっていますか? –

+0

私は新しいアプリケーションをテストし、フレームワークを構築しています。注意書きが記録されている瞬間、私は構成メソッドが失敗したためにテストがスキップされていることがわかります。なぜ、ログ情報を追加する必要があるのですか。 –

答えて

1

2人のリスナーをマージする必要があります。

public class MyNewListener implements IConfigurationListener2, ITestListener { 

    private final ThreadLocal<ITestResult> currentTest = new ThreadLocal<>(); 

    @Override 
    public void onTestStart(ITestResult result) { 
    System.out.println("Starting test method:" + result.getMethod().getMethodName()); 
    currentTest.set(result); 
    } 

    @Override 
    public void onConfigurationFailure(ITestResult result) { 
    System.out.println("Failed to run " + result.getMethod().getMethodName() 
      + " for the test method:" + currentTest.get().getMethod().getMethodName()); 
    } 
} 

ThreadLocalは、パラレル実行を追跡するために使用されます。

免責事項:私は実生活でリスナーをテストしませんでした。それが働いているかどうか私に教えてください。

編集:テストではなく、現在ログインしている何を失敗したコンフィギュレーション方法

public class MyNewListener implements IConfigurationListener2, ITestListener { 

    private final ThreadLocal<ITestResult> failedConfiguration = new ThreadLocal<>(); 

    @Override 
    public void onTestStart(ITestResult result) { 
    System.out.println("Starting test method:" + result.getMethod().getMethodName()); 
    } 

    @Override 
    public void onConfigurationFailure(ITestResult result) { 
    failedConfiguration.set(result); 
    } 

    @Override 
    public void onTestSkipped(ITestResult result) { 
    System.out.println("Failed to run " + failedConfiguration.get().getMethod().getMethodName() 
      + " for the test method:" + result.getMethod().getMethodName()); 
    } 
} 
+0

残念ながら、これは現在機能しました。これは 'onConfigurationFailure()'が 'onTestStart()'メソッドの前に呼び出され、nullを返します。ヘルプthoをありがとう。 –

+0

@ T.Daves Ok!私はちょうど別の命題で私の答えを更新しました。その結果を教えてください! :) – juherr

+0

私は編集された答えが働いたことを確認することができます。私は昨日似たようなことをして、今や '@ AfterMethod'に取り組んで、同様の方法でこれを記録します。 –

関連する問題