.NET自动化测试---API轻量级测试

自动化测试相对于手工测试有以下优点

a.Speed--快速地进行成千上万个测试用例;

b.Accuracy--不受人为的因素的干扰,例如记录错误的结果;

c.Precision--每次都以同样的方式运行;

d.Efficiency--晚上白天都可以进行测试;

e.Skill-Building--培养技能,手工测试非常枯燥乏味,提高不了技能。

       我们知道API(Application Programing Interface)测试是软件自动化测试的基础,API测试用来验证组成软件的那些单个方法的正确性;API测试也称为单元测试-Unit Test、模块测试-Module Test、组件测试-Component Test和元件测试-Element Test。 不管怎么叫,背后的意思是:必须确定系统中每个单独的模块必须正常工作。

待测方法的代码如下:

View Code
1 public static double ArihthmeticMean(double i,double j)
2 {
3 double result=0.0;
4 result = Math.Max(i,j);
5 return result;
6 }

好了,下面来介绍API自动化测试的流程:

1、准备测试用例数据,最好是独立于测试套件,可以存放在txt文件 ,XML文件和数据库,以便多个测试套件重复使用这里我准备好了

测试ID    待测方法    测试输入  期望值

0001:ArihthmeticMean:4 8:8
0002:ArihthmeticMean:2 2:24
0003:ArihthmeticMean:4 8:8
0004:ArihthmeticMean:3 2:345.4
0005:ArihthmeticMean:55 555:555
0006:ArihthmeticMean:2 444:23234
0007:ArihthmeticMean:4 8:8
0008:ArihthmeticMean:2 2:24

实际测试时,需要成千上万个测试用例数据,包括边界值,空值,无效输入。有些自动化测试工具可以自动产生测试用例数据。

2、读入测试用例数据

a.如何从测试用例文件中读入读入每条测试用例数据?

b.设计--通过while循环遍历测试用例的每一行。

c.方案 

View Code
1 while ((line =sr.ReadLine())!=null)
2 {
3 //解析每个测试用例行
4
5 //调用待测方法
6
7 //判断是否通过
8
9 //记录测试用例结果
10
11 //发送邮件等等通知结果
12  
13 }

3、解析测试用例

a.如何解析出用字符串隔开的测试用例的各个字段

b.设计-使用string.Split()方法,该方法返回一个字符数组。具体使用去查查api

c.具体实现方案看接下来的代码

4、把数据转换为合适的类型

a.读入的是字符串类型,在这里必须转为整型

b.看下面的整理代码

5、判定测试用例通过与否

a.如何判定API测试用例是否通过

b.设计--调用待测方法,传给它测试用例的输入,得到返回值,然后比较实际结构和测试用例中读入的期望值是否一致

c.具体代码看下面

6、记录测试用例结果

a.如何把测试用例的结果存入独立于测试程序的简单文本文件

b.设计--在处理测试用例的主循环中,使用System.IO.StreamWriter对象把测试用例ID和测试结果写到一个文本文件。当然可以记录到XML文件或者数据库中,看需要

c.代码实现看下面

7、给测试用例结果文件加上实际戳

 

View Code
1 string stamp = DateTime.Now.ToString();
2 stamp = stamp.Replace(":","-");//这里不进行符号替换,创建不来文件,以为:这个符号不能用于创建文件问
3   fs=File.Create("E:\\C#\\C#.NET例子程序\\"+stamp +" testresult.txt");

8、通过计算对测试结果进行总结

9、获得测试的总运行时间

    用DateTime TimeSpan可以实现

10、处理输入为空或者期望值为空的情况--NULL

11、处理“方法抛出异常”的情况

12、处理输入参数为空字符串的情况

13、在测试用例失败时发送警告邮件

      System.Net.Mail下的MailMessage类实现,很简单

14、自动运行测试套件

用.BAT文件可以实现,用System.Diagnosis.Process命名空间下的Start()方法

  

在这里,稍微实现了代码



View Code
1 using System;
2  using System.IO;
3
4 namespace ApiTestExample
5 {
6 class Program
7 {
8 static void Main(string[] args)
9 {
10 FileStream ifs=null;
11 StreamReader sr = null;
12 StreamWriter sw = null;
13 FileStream fs = null;
14 try
15 {
16 ifs = new FileStream("E:\\C#\\C#.NET例子程序\\testcase.txt", FileMode.Open);
17 sr = new StreamReader(ifs);
18 string stamp = DateTime.Now.ToString();
19 stamp = stamp.Replace(":","-");//这里不进行符号替换,创建不来文件,以为:这个符号不能用于创建文件问
20 fs=File.Create("E:\\C#\\C#.NET例子程序\\"+stamp +" testresult.txt");
21 sw = new StreamWriter(fs);
22 string line, caseID, method;
23 string[] tokens, tempInput;
24 string expected;
25 double actual = 0.0;
26 int numPass = 0, numFail = 0;
27
28 Console.WriteLine("\nCaseID Result Method Details");
29 Console.WriteLine("===============================");
30
31 while ((line =sr.ReadLine())!=null)
32 {
33 tokens = line.Split(':');
34 caseID =tokens [0];
35 method =tokens[1];
36 tempInput =tokens[2].Split (' ');
37 expected=tokens [3];
38
39 int[] input=new int[tempInput.Length ];
40 for (int i = 0; i < input.Length ; i++)
41 {
42 input[i] = int.Parse(tempInput [i]);
43 }
44
45 if (method == "ArihthmeticMean")
46 {
47 actual = ArihthmeticMean(input[0], input[1]);
48 if (actual.ToString() == expected)
49 {
50 Console.WriteLine(caseID + " Pass " + method + " actual=" + actual.ToString());
51 //记录测试
52 sw.WriteLine(caseID + " Pass " + method + " actual=" + actual.ToString());
53 ++numPass;
54 }
55 else
56 {
57 Console.WriteLine(caseID + " Fail " + method + " actual=" + actual.ToString());
58 //记录测试
59 sw.WriteLine(caseID + " Fail " + method + " actual=" + actual.ToString());
60 ++numFail;
61 }
62
63 }
64 else
65 {
66 Console.WriteLine(caseID +" "+method +"not yet implement");
67 sw.WriteLine(caseID + " " + method + "not yet implement");
68 }
69
70 }
71
72 Console.WriteLine("===========================end test run===============");
73 Console.WriteLine("pass="+numPass +"Fail="+numFail );
74 }
75 catch(Exception ex)
76 {
77 Console.WriteLine("Fatal error"+ex.Message );
78 }
79 finally
80 {
81 //注意打开的文件资源一定要关闭,还有注意顺序
82 ifs.Close();
83 sr.Close();
84 sw.Close();
85 fs.Close();
86
87
88 }
89
90
91 }
92
93 //待测方法ArihthmeticMean
94 public static double ArihthmeticMean(double i,double j)
95 {
96 double result=0.0;
97 result = Math.Max(i,j);
98 return result;
99 }
100 }
101 }

结果如下:
CaseID Result Method Details
===============================
0001  Pass  ArihthmeticMean  actual=8
0002 Fail ArihthmeticMean actual=2
0003  Pass  ArihthmeticMean  actual=8
0004 Fail ArihthmeticMean actual=3
0005  Pass  ArihthmeticMean  actual=555
0006 Fail ArihthmeticMean actual=444
0007  Pass  ArihthmeticMean  actual=8
0008 Fail ArihthmeticMean actual=2
===========================end test run===============
pass=4Fail=4

测试结果文件2011-3-26 16-01-02  testresult.txt,内容如下

0001  Pass  ArihthmeticMean  actual=8
0002 Fail ArihthmeticMean actual=2
0003  Pass  ArihthmeticMean  actual=8
0004 Fail ArihthmeticMean actual=3
0005  Pass  ArihthmeticMean  actual=555
0006 Fail ArihthmeticMean actual=444
0007  Pass  ArihthmeticMean  actual=8
0008 Fail ArihthmeticMean actual=2



至此,API自动化测试介绍基本完毕,请多多指教,谢谢!


posted @ 2011-03-26 16:03  蓝鸿鹄  阅读(2919)  评论(3编辑  收藏  举报