2017-03-23 3 views
0

TestNGの@afterメソッドでは、スタックトレースをキャプチャしてエクステントレポートに含めたいと思っていますが、良い解決策を見つけることができません。私は、スタックトレースを印刷する上で、たくさんのスレッドがあることを認識していますが、私が望むものを私に与えているものはありません。私は、失敗の原因となったメソッドと行番号が必要です。私はスタックトレースが巨大であるかどうか気にしません。なぜなら、失敗した行までのリードを見たいと思うからです。ここで TestNGの@afterメソッドからエクステントレポートにスタックトレースを書き込む

は、私はエクステントレポートに書き込む場所の例です:

@AfterMethod(alwaysRun = true) 
    public void afterMethod(ITestResult result, Method method) throws InterruptedException { 

     ExtentTest test = ExtentTestManager.getTest(); 

     // Get thread id 
     long id = Thread.currentThread().getId(); 

     if (result.getStatus() != ITestResult.SKIP) 
     { 

      String os = StoredVariables.getos().get(); 

      String resultStatus = ""; 
      // Write passing tests to Extent Reports 
      if (result.getStatus() == ITestResult.SUCCESS) { 
        test.log(LogStatus.PASS, "<span class='label success'>" + result.getName() + "</span>"); 
        resultStatus = "Passed"; 
        StoredVariables.getfailedTest().set(false); 
       } 

      // Write failing tests to Extent Reports 
      if (result.getStatus() == ITestResult.FAILURE) { 
       test.log(LogStatus.FAIL, "<span class='label failure'>" + result.getName() + "</span>", "<pre>Results = " + result.getThrowable().getCause() + "\n\n" + result.getThrowable().getMessage() + "</pre>"); 
       resultStatus = "Failed"; 
       StoredVariables.getfailedTest().set(true); 
      } 

      System.out.println("TEST RESULT: " + method.getName() + " - Thread " + id + " = " + resultStatus); 

Thread.currentThread().getStackTrace()は私を与える "[Ljava.lang.StackTraceElement; 2e1ef60 @"

印刷ItestResult resultが直接近づく私を取得しますから

[TestResult name=functionChecks status=FAILURE method=ExampleScripts.functionChecks()[pri:0, instance:[email protected]] output=Completed executing following method : ExampleScripts.functionChecks]

答えて

0

単純にスロー可能なオブジェクトをログに渡し、例外が出力されます。正しく

リファレンス:http://extentreports.com/docs/versions/3/java/#logging-exceptions

test.fail(result.getThrowable()); 

溶液は、ドキュメントのTeatNGの実施例の節においても利用可能です。

+0

result.getThrowable()を使用すると、 "java.lang.AssertionError:Failed expected [true]が見つかりましたが、[false]"が表示されます。まだ行番号がなく、私は失敗の原因をキャプチャしようとしていますが、必ずしも最後のメソッド呼び出し –

+0

@DustinNとは限りません。 Hei。私は同じresult.getThrowable()を使用しており、ExtentReportsログに完全なスタックトレースを取得しています。どの程度のレポートバージョンを使用していますか? –

+0

私はExtent Reportsバージョン2.41.0とSelenium 2.53.0を使用しています。 –

関連する問題