Java自動化測試框架(TestNG)——異常測試

Java自動化測試框架(TestNG)——異常測試

在使用 TestNG 進行測試時,有些場景,我們通過向測試方法傳入某些異常的參數,期望代碼拋出異常時,我們可以通過 @Test( expectedExceptions,
expectedExceptionsMessageRegExp)實現,並且可以實現異常信息的斷言。


運行時異常與檢查異常

Java中 對於異常分為運行時異常與檢查異常。

運行時異常,編譯時不被檢查的異常,不需要用throws 聲明拋出 異常對象所屬類,也可以不用throw 拋出異常對象或異常引用。對於調用該方法,也不需要放於 try-catch 代碼塊中。(避免程序代碼錯誤被掩蓋在運行中無法察覺)

檢查異常,編譯時被檢測的異常,一旦用throw 拋出異常,如果當前方法可處理異常,那麼需要在該方法內通過 try-catch 處理異常。如果當前方法不具備該異常的處理能力,那麼必須在參數列表後方法體 前使用 throws 聲明異常所屬類,交給方法的調用方處理 。


運行時異常測試(RuntimeException)

首先先創建一個自定義運行時異常類 CustomRuntimeException,如下

<code>

package

testng.base.demo; ​

public

class

CustomRuntimeException

extends

RuntimeException

{ ​        

public

CustomRuntimeException

()

{        

super

();   } ​        

public

CustomRuntimeException

(String message)

{        

super

(message);   } ​        

public

CustomRuntimeException

(String message, Throwable cause)

{        

super

(message,cause);   } ​        

public

CustomRuntimeException

(Throwable cause)

{        

super

(cause);   } ​ }/<code>

創建一個測試類:runtimeExceptionTest.jav ,其代碼如下所示 -

<code>

public

class

runtimeExceptionTest

{        

public

void

testExceptionDemo

()

{        

throw

new

CustomException(

"TestNG custom RuntimeException."

);   } ​ } ​/<code>

執行該測試用例,將拋出如下異常信息

<code>

testng

.base

.demo

.CustomException

:

TestNG

custom

RuntimeException

. ​

at

testng

.base

.demo

.runtimeExceptionTest

.testExceptionDemo

(

runtimeExceptionTest

.java

:7)

at

org

.testng

.SuiteRunnerWorker

.runSuite

(

SuiteRunnerWorker

.java

:52)

at

org

.testng

.SuiteRunnerWorker

.run

(

SuiteRunnerWorker

.java

:86)

at

com

.intellij

.rt

.testng

.IDEARemoteTestNG

.run

(

IDEARemoteTestNG

.java

:66)

at

com

.intellij

.rt

.testng

.RemoteTestNGStarter

.main

(

RemoteTestNGStarter

.java

:110)

   /<code>

我們期望結果就是拋出該異常信息,但此時用例執行失敗了。與我們想要的期望在拋出異常時,用例執行成功的結果不一致,此時我們可以在 @Test註解中通過expectedExceptions參數實現,如下:

<code>

package

testng.base.demo;

import

org.testng.annotations.Test; ​

public

class

runtimeExceptionTest

{     (expectedExceptions = CustomRuntimeException

.

class

)    

public

void

testExceptionDemo

()

{        

throw

new

CustomRuntimeException(

"TestNG custom RuntimeException."

);   } ​ }/<code>

此時,執行測試用例時,執行結果是成功的,如下

<code>

===============================================

Default

Suite

Total tests run:

1

,

Failures:

0

,

Skips:

0

===============================================

   /<code>

我們還可以對拋出異常的信息進行判斷,同樣在@Test註解中通過
expectedExceptionsMessageRegExp參數實現,如下:

<code>

package

testng.base.demo;

import

org.testng.annotations.Test; ​

public

class

runtimeExceptionTest

{         (expectedExceptions = CustomRuntimeException

.

class

,

expectedExceptionsMessageRegExp

=

"TestNG custom RuntimeException."

)    

public

void

testExceptionDemo

()

{        

throw

new

CustomRuntimeException(

"TestNG custom RuntimeException."

);   }     } ​/<code>

當拋出的異常信息與
expectedExceptionsMessageRegExp參數不一致時,用例將運行失敗。


檢查異常測試(Exception)

首先先創建一個自定義檢查異常類 CustomException ,如下

<code>

package

testng.base.demo; ​

public

class

CustomException

extends

Exception

{ ​        

public

CustomException

()

{        

super

();   } ​        

public

CustomException

(String message)

{        

super

(message);   } ​        

public

CustomException

(String message, Throwable cause)

{        

super

(message,cause);   } ​        

public

CustomException

(Throwable cause)

{        

super

(cause);   } ​ } ​/<code>

當進行檢查類異常測試時,只需要在測試方法的參數列表後方法體 前用 throws 聲明異常所屬類,代碼如下

<code>

package

testng.base.demo;

import

org.testng.annotations.Test; ​

public

class

exceptionTest

{ ​     (expectedExceptions = CustomException

.

class

,

expectedExceptionsMessageRegExp

=

"TestNG custom Exception."

)    

public

void

testExceptionDemo

()

throws

CustomException

{        

throw

new

CustomException(

"TestNG custom Exception."

);   } ​ } ​/<code>



分享到:


相關文章: