前面簡要講了關於FIT的用法(參看FIT For .NET(0) ,FIT For .NET(1) FIT FOR .NET(2)),在接下來的文章中,我將詳細介紹FIT的用法.由於FIT的不斷更新,其文檔也是不斷進步,所以為了得到最近的資訊,請訪問http://fit.c2.com .
首先介紹ColumnFixture:
Coulmn映射測試資料的列到子類的方法或變數.
一個新的Column fixture為每個使用它的表建立,同樣的Column fixture會從上到下處理每行,從左至右處理每列.CalculatorExample 這個例子就是這樣.x()因數時第一個方法列,不管Key如何改變,都可以得到檢查
你可以參看一下資源:
- http:Release/Source/fit/Fixture.java
- http:Release/Source/fit/ColumnFixture.java
還是舉個例子詳細解釋一下吧.
拿個算術的例子.這個算術包括加減乘除.
我們可以寫測試案例為(以表的形式):
| eg.ArithmeticColumnFixture |
| x |
y |
plus() |
times() |
divide() |
floating() |
| 2 |
3 |
5 |
6 |
0 |
0.6666667 |
| 0 |
0 |
0 |
0 |
error |
error |
| 0 |
0 |
0 |
0 |
|
|
| 200 |
300 |
500 |
60000 |
0 |
0.6666667 |
| 2 |
3 |
10 |
10 |
10 |
| 200 |
3 |
5 |
6 |
0 |
0.6666667 |
| 2 |
-3 |
-1 |
-6 |
-0 |
-0.6666667 |
然後寫出載入FIT架構的代碼:
1// Copyright (c) 2002 Cunningham & Cunningham, Inc.
2// Released under the terms of the GNU General Public License version 2 or later.
3
4using System;
5using fit;
6
7namespace eg
8{
9 public class ArithmeticColumnFixture : ColumnFixture
10 {
11
12 public int x;
13 public int y;
14
15 public int plus()
16 {
17 return x + y;
18 }
19
20 public int minus()
21 {
22 return x - y;
23 }
24
25 public int times ()
26 {
27 return x * y;
28 }
29
30 public int divide ()
31 {
32 return x / y;
33 }
34
35 public float floating ()
36 {
37 return (float)x / (float)y;
38 }
39
40 public ScientificDouble sin () {
41 return new ScientificDouble(Math.Sin(toRadians(x)));
42 }
43
44 public ScientificDouble cos () {
45 return new ScientificDouble(Math.Cos(toRadians(x)));
46 }
47
48 private double toRadians(double degrees) {
49 return (degrees * Math.PI) / 180d;
50 }
51 }
52}
下面解釋一下:
注意到表的第一行:
eg.ArithmeticColumnFixture
什麼意思?看看原始碼,你發現了什麼呢?,沒錯,表的第一行就是原始碼所描述的類的名稱.你可能已經想到這個類的方法和表的關係了.
很明顯,表的x,y分別對應代碼12行的public int x;13行的public int y;(這裡x,y必須為public).
後面的plus(),times(),divide(),floating()那就分別對應類的其他方法了.
所以,我們可以想象得到FIT架構其實就是讀取以表格形式描述的客戶測試案例的資料到測試類別裡面去,然後配上NUnit的協作,最終完成了自動化測試.