主要方法
countTestCases:統計
TestCases 數目
run:運行測試并將結果返回到指定的TestResult 中
Class Assert 首先,Assert 提供的public 方法都可以帶或不帶自己定義的提示,其次Assert
中的Assert 方法是protected 的,這意外著Assert 是一個靜態類,它提供的方法
都是Static 的。
public 方法:
assert:保留(deprecated)方法,判斷一個條件是否為真
assertTrue:assert 的替代方法,判斷一個條件是否為真
assertEquals:用于判斷實際值和期望值是否相同(Equals),可以是各種JAVA
對象。
assertNotNull:判斷一個對象是否不為空
assertNull:判斷一個對象是否為空
assertSame:判斷實際值和期望值是否為同一個對象(="=),注意和assertEquals 區分
fail:直接返回失敗,拋出AssertionFailedError
private 方法:
failNotEquals:主要用于assertEquals 方法,調用fail 返回失敗提示
failNotSame:主要用于assertSame 方法,調用fail 返回失敗提示
Class AssertionFailedError
AssertionFailedError 是從Jdk 提供Error 類簡單繼承而來,主要方法如下:
public AssertionFailedError (String message) {
super (message);
}
Class Assert 中比較失敗都是拋出AssertionFailedError。
Interface Protectable
這個接口是使用了一種比較少見的用法。
在Interface 本身只定義了一個方法
public abstract void protect() throws Throwable;
注意方法throws 的是所有Error 和Exception 的祖先。通過這種定義可以保
證運行的時候如果出現任何Error 和Exception,都將被拋出而不會導致程序不能
繼續運行。
Portectable 的接口沒有被framework 包中的任何類實現,它的使用在類
TestResult 中的run 方法中。以下是run 方法中代碼:
protected void run(final TestCase test) {
startTest(test);
Protectable p= new Protectable() {
public void protect() throws Throwable {
test.runBare();
}
};
runProtected(test, p);
endTest(test);
}
這里實際是聲明了一個Anonymous Classes,實現了Interface Portectable
Interface TestListener
TestListener 的用途和它名稱一樣,用于監聽。主要用于運行時刻監聽,
BaseRunner(所有運行類,如TestRunner)實現了這一接口。由于運行是通過
TestResult 來實現,只要調用TestResult.addListener 就可以增加監聽,TestResult
會調用接口中相應的方法,具體見TestResult。
主要方法:
public
addError:增加錯誤,注意這里錯誤應該指測試程序本身的錯誤或者被測試程
序錯誤,而不是測試失敗
addFailure:增加一個測試失敗,專用于AssertionFailedError 的處理
endTest:結束測試
startTest:開始測試
Class TestCase
使用者最主要使用的類,繼承Class Assert,實現Interface Test。主要方法
public
TestCase:創建本身,可以指定TestCase 準備運行的測試方法名稱,保存在私
有屬性fName。
countTestCases:返回TestCase 數目,直接返回1
name:deprecated,建議使用getName,返回TestCase 當前準備允許的測試方法
的名稱(私有屬性fName)
run:運行TestCase,如果沒有指定結果存儲的TestResult,將調用createResu(lt
方法。注意,TestCase 與TestResult 會有互相調用。整個運行流程如下:
1、TestCase.run 調用TestResult.run
2、TestResult.run 調用TestResult .StartTest
3、TestResult.run 創建一個Anonymous 類,實現接口Portectable
4、在Portectable. protect 方法中調用TestCase .runBare
5、通過運行Portectable.runBare 調用runBare,通過Exception 捕獲增加錯誤
及失敗報告
runBare:不使用TestResult 直接運行
runTest:運行測試,注意每調用runTest 只運行當前fName 指定的方法
getName:返回fName
setName:設置fName
protected
createResult:創建一個TestResult
setUp:在運行runTest 前調用
tearDown:在運行runTest 后調用
Class TestFailure
用于存放測試對比失敗信息的類。主要為Class TestResult 調用。主要屬性
protected Test fFailedTest;
protected Throwable fThrownException;
fFailedTest 存放失敗的TestCase 信息,fThrownException 存放失敗提示信息。
主要方法:
public
TestFailure:初始化,對fFailedTest、fThrownException 賦值。
failedTest:返回fFailedTest
thrownException:返回fThrownException
toString:
Class TestResult
TestResult 用于運行并收集測試結果(通過Exception 捕獲),注意interface
TestListener 的所有方法在這里都有同名方法并在同名方法中被調用。
主要屬性:
protected Vector fFailures:測試失敗報告保存
protected Vector fErrors:測試錯誤報告保存
protected Vector fListeners:測試監聽器保存
protected int fRunTests:運行的測試
private boolean fStop:是否應該停止測試標志,由stop 方法設置
文章來源于領測軟件測試網 http://www.kjueaiud.com/