長期以來,在軟件開發中我們一直關心著兩個主要問題:
第一,業務如何通過應用程序與其所需內容通信;
第二,工程師如何驗證他們是否正在構建滿足業務需要的正確軟件。多年來,為了解決這些關心的問題,已探索了許多方法和框架,但直到出現 Framework for Integrated Tests (FIT) 以后,才找到了解決這些問題的簡便而直觀的方法。
使用FIT我們可以編寫出可以自動運行的確認測試用例,可以用來確認我們所開發出來的軟件是否滿足了用戶所需的功能,可以作為持續構建過程的一部分來確保所構建出來的版本是正確的。但是,FIT還有另外一個更為重要的功能,那就是在軟件開發中增強協作,尤其是開發團隊和客戶、領域專家之間的協作。這種協作可以有效地降低軟件開發中的不必要的復雜性,加速反饋,并確保最大程度地為客戶提供最高的價值。
FIT如何工作
簡單來講,FIT就是一個軟件,它能夠讀取HTML文件中的表格(這些表格可以通過MicroSoft Word或者Excel產生)。針對每個表格,都會由一個程序員編寫的"fixture"(裝置)來解釋。該fixture會驅動“被測系統 (SUT?System Under Test)”來對表格中給出的測試用例進行檢驗。
Fixture充當Fit表格和要測試系統間的媒介,起協調作用,完成表格中給出的測試。FIT中提供了好幾種類型的Fixture,它們分別用于處理不同的情形。Fixture的形式有3種:
ColumnFixture(對應于“列”表),“列”表的形式如下圖所示:
CalculateScholarship
Score Scholarship()
1000 0
1999 0
2000 500
2050 500
2100 1000
2200 1500
2300 2000
2350 2000
2400 2500
RowFixture(對應于“行”表),“行”表的形式如下圖所示:
DiscountGroupOrderedList
order future value max owing min purchase discount percent
1 low 0.00 0.00 0
2 low 0.00 2000.00 3
3 medium 500.00 600.00 3
4 medium 0.00 500.00 5
5 high 2000.00 2000.00 10
ActionFixture,表明以表格給出的測試用例的一系列的操作步驟。見表1。
表1
fit.ActionFixture
start cstc.fitexam.coffeemaker.AddInventory
enter units coffee 3
enter units milk 5
enter units sugar 6
enter units chocolate 7
check coffee inventory 18
check milk inventory 20
check sugar inventory 21
check chocolate inventory 22
在表1中,第1列給出了執行的命令,這里共有3個命令,但是其它的命令可以根據實際情況在ActionFixture.的子類中進行創建。上述的3個命令是:
Start:與該Fixture相關聯的類的名稱
Enter:該類的一個方法(帶有一個變量)
Check:該類的一個方法的返回值(不帶變量)
為該表格創建一個ActionFixture類如下:
package cstc.fitexam.coffeemaker;
import fit.ActionFixture;
public class AddInventory extends ActionFixture {
private CoffeeMaker cm = new CoffeeMaker();
private Inventory i = cm.checkInventory();
public void unitsCoffee(int coffee) {
cm.addInventory(coffee,0,0,0);
}
public void unitsMilk(int milk) {
cm.addInventory(0,milk,0,0);
}
public void unitsSugar(int sugar) {
cm.addInventory(0,0,sugar,0);
}
public void unitsChocolate(int chocolate) {
cm.addInventory(0,0,0,chocolate);
}
public int coffeeInventory() {
return i.getCoffee();
}
public int milkInventory() {
return i.getMilk();
}
public int sugarInventory() {
return i.getSugar();
}
public int chocolateInventory() {
return i.getChocolate();
}
}
此 fixture 將調用下面清單中顯示的 SUT(被測對象) CoffeeMaker 類,然后調用該類上的相關方法。
package cstc.fitexam.ffeemaker;
public class CoffeeMaker {
/**
* Array of recipes in coffee maker
*/
private Recipe [] recipeArray;
/** Number of recipes in coffee maker */
private final int NUM_RECIPES = 4;
/** Array describing if the array is full */
private boolean [] recipeFull;
/** Inventory of the coffee maker */
private Inventory inventory;
/**
* Constructor for the coffee maker
*
*/
public CoffeeMaker() {
recipeArray = new Recipe[NUM_RECIPES];
recipeFull = new boolean[NUM_RECIPES];
for(int i = 0; i < NUM_RECIPES; i++) {
recipeArray[i] = new Recipe();
recipeFull[i] = false;
}
inventory = new Inventory();
}
/**
* Returns true if a recipe is successfully added to the
* coffee maker
* @param r
* @return boolean
*/
public boolean addRecipe(Recipe r) {
boolean canAddRecipe = true;
//Check if the recipe already exists
for(int i = 0; i < NUM_RECIPES; i++) {
if(r.equals(recipeArray[i])) {
canAddRecipe = false;
}
}
//Check for an empty recipe, add recipe to first empty spot
if(canAddRecipe) {
int emptySpot = -1;
for(int i = 0; i < NUM_RECIPES; i++) {
if(!recipeFull[i]) {
emptySpot = i;
canAddRecipe = true;
}
}
if(emptySpot != -1) {
recipeArray[emptySpot] = r;
recipeFull[emptySpot] = true;
}
else {
canAddRecipe = false;
}
}
return canAddRecipe;
}
文章來源于領測軟件測試網 http://www.kjueaiud.com/