Fitnesse FIT的使用


FIT是fitnesse使用的默認的引擎(SLIM的使用在上一篇文章中說明),不需要特別聲明即可使用執行表格測試,所有編寫的fixture都需要繼承FitFitxture

編寫測試用例前需要先聲明class或者jar包所在的路徑,才能找到所需要的fixture

使用關鍵字path

1. Column Fixture

這是使用最多的,每一行代表輸入或者期望輸出,添加?的代表調用的輸出方法,如果期望值和實際輸出值一致,則顯示綠色,否則顯示紅色,並且顯示實際輸出值。如果添加的是()表示返回值,值的顏色是灰色;addRemovePayerFixture在包fixture下,調用的時候需要加上包路徑

源碼:

public class AddRemovePlayerFixture extends ColumnFixture {

    private String playerName;

    private Game theGame;

    public void setPlayerName(final String playerName) {

        this.playerName = playerName;

    }

    public boolean addPlayer() {

        this.theGame = StaticGame.theGame;

        Player thePlayer = this.theGame.addPlayer(this.playerName);

        return this.theGame.playerIsPlaying(thePlayer);

    }

    public int countPlayers() {

        return this.theGame.getNumberOfPlayers();

    } 

}

    輸入的playerName會調用set方法載入輸入值

 

2. Row Fixture

用於查詢數據的fixture,只要輸入了查詢的條件就可以獲得返回,也可以校驗輸出的返回值,這種表格適用於查詢數據

 

源碼

public class EmployeePayRecordsRowFixture extends RowFixture {

    public Object[] query() throws Exception {

        EmployeePayRecord[] records = new EmployeePayRecord[2];

        records[0] = new EmployeePayRecord(1, 1000, "Bob");

        records[1] = new EmployeePayRecord(2, 2000, "Jack");

        return records;

    }

    public Class getTargetClass() {

        return EmployeePayRecord.class;

    }

}

public class EmployeePayRecord {

    public int id;

    private double salary;

    public String name;

    public EmployeePayRecord(final int id, final double salary, final String name) {

        this.id = id;

        this.salary = salary;

        this.name = name;

    }

    public double pay() {

        return this.salary;

    }

}

fixture先通過getTargetClass()加載查詢的數據對象,然后通過query獲得查詢結果,整個fixture的作用就是組裝數據,

查詢的時候從左往右匹配條件,如果前面的列值沒有找到對應的數據,則會標記為fail,查詢結果中,沒有期望的值,則會標記為miss,期望值不在查詢結果中,則會標記為surplus

 

3. Action Fixture

當想要操作一系列的方法的時候,可以使用該fixture

操作的類型主要有三種enter,press,check

enter: 一般適用於set方法,把期望的值傳遞給fixture

press:執行方法,參數可選

check:需要輸入期望值

 

源碼:

public class CountFixture extends Fixture {

    private int counter = 0;

    public void count() {

        this.counter++;

    }

    public int counter() {

        return this.counter;

    }

    public void setCounter(final int num) {

        this.counter = num;

    }

}

 

4.Table Fixture

當fit提供的fixture不能滿足需要的時候,可以使用table fixture,該fixture可以自由處理表格單元格的

(0,0)表示左上角第一個單元格

(row,column)都是從0開始

Table fixture的方法

protected abstract void doStaticTable(int rows)

Table   Fixture is an abstract class that   you must derive from. You must override doStaticTable to perform the   functions of the fixture. The number of rows in the table is passed in rows.

protected Parse getCell(int row, int column)

Returns the addressed table   cell as a Parse.

protected String getText(int row, int column)

Returns the text within the   addressed table cell.

protected boolean blank(int row, int column)

Returns true if the   addressed table cell is blank.

protected void wrong(int row, int column)

Turns the addressed table   cell red.

protected void right(int row, int column)

Turns the addressed table   cell green.

protected void wrong(int row, int column, String actual)

Turns the addressed table   cell red, and annotates it with the actuall value.

protected void ignore(int row, int column)

Turns the addressed cell   gray.

protected int getInt(int row, int column)

Converts the addressed cell   to an int, and returns it.

 

官網上的例子:http://www.fitnesse.org/FitNesse.UserGuide.WritingAcceptanceTests.FitFramework.TableFixture

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM