2013-06-03 43 views
1

我想知道是否有任何方法來定製超時異常錯誤消息。超時時發生TestNG自定義錯誤消息

我的測試是通過使用DataProvider進行參數化的,所以我想在錯誤消息中包含有關參數的信息,以便了解特定的場景失敗。

錯誤消息

Method org.testng.internal.TestNGMethod.testEndpoint() didn't finish within the time-out 8000 

堆棧跟蹤

org.testng.internal.thread.ThreadTimeoutException: Method  
org.testng.internal.TestNGMethod.testEndpoint() didn't finish within the time-out 8000 
    at java.net.SocketInputStream.socketRead0(Native Method) 
    at java.net.SocketInputStream.read(SocketInputStream.java:150) 
    at java.net.SocketInputStream.read(SocketInputStream.java:121) 
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:442) 
    at sun.security.ssl.InputRecord.read(InputRecord.java:480) 
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927) 
    at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:884) 
    at sun.security.ssl.AppInputStream.read(AppInputStream.java:102) 
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235) 
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:275) 
    at java.io.BufferedInputStream.read(BufferedInputStream.java:334) 
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:633) 
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579) 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1322) 
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468) 
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338) 

回答

0

可以通過實現該接口ITestListener和onTestFailure創建監聽器類()獲得使用getThrowable()

String stackTraceString =  Throwables.getStackTraceAsString(getCurrentTestResult().getThrowable()); 

在異常你應該以同樣的方式解決問題用setThrowable()修改錯誤文本(不檢查自己)。

0

您可以從TestResult對象獲取您的數據提供者參數。正如Vlad所建議的那樣,實現ITestListener接口。

在onTestFailure中,使用result.getParameters獲取參數,並使用getThrowable獲取異常。用你的自定義消息創建一個新的Throwable,追加你的參數並且可能附加現有的消息(它有一個構造函數),然後用result.setThrowable將這個Throwable設置爲結果對象。

相關問題