超时时TestNG自定义错误消息

时间:2013-06-03 17:36:26

标签: java unit-testing 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)

2 个答案:

答案 0 :(得分:0)

您可以通过实现接口ITestListener创建一个侦听器类,并在onTestFailure()中使用getThrowable()

获取异常
String stackTraceString =      Throwables.getStackTraceAsString(getCurrentTestResult().getThrowable());

以同样的方式,您应该能够使用修改过的错误发短信来设置Throwable()(不检查我自己)。

答案 1 :(得分:0)

您可以从TestResult对象获取dataprovider参数。正如Vlad建议的那样,实现ITestListener接口。

在onTestFailure中,使用result.getParameters获取参数并使用getThrowable获取异常。使用自定义消息创建一个新的Throwable,附加params并可能附加现有消息(有一个构造函数),然后使用result.setThrowable将此Throwable设置为结果对象。