FIT FOR .NET(3)
前面简要讲了关于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如何改变,都可以得到检查
你可以参看一下资源:
还是举个例子详细解释一下吧.
拿个算术的例子.这个算术包括加减乘除.
我们可以写测试案例为(以表的形式):
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
4
using System;
5
using fit;
6
7
namespace 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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

下面解释一下:
注意到表的第一行:
eg.ArithmeticColumnFixture
什么意思?看看源代码,你发现了什么呢?,没错,表的第一行就是源代码所描述的类的名称.你可能已经想到这个类的方法和表的关系了.
很明显,表的x,y分别对应代码12行的public int x;13行的public int y;(这里x,y必须为public).
后面的plus(),times(),divide(),floating()那就分别对应类的其他方法了.
所以,我们可以想象得到FIT框架其实就是读取以表格形式描述的客户测试案例的数据到测试类里面去,然后配上NUnit的协作,最终完成了自动化测试.