【转】.NET自动化测试---API轻量级测试
上一篇 / 下一篇 2011-10-18 11:45:23 / 个人分类:API测试
转自:http://www.cnblogs.com/chester/archive/2011/03/26/1996357.html
自动化测试相对于手工测试有以下优点
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。 不管怎么叫,背后的意思是:必须确定系统中每个单独的模块必须正常工作。
待测方法的代码如下:
1publicstaticdoubleArihthmeticMean(doublei,doublej)
2{
3doubleresult=0.0;
4result=Math.Max(i,j);
5returnresult;
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.方案
1while((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、给测试用例结果文件加上实际戳
1stringstamp=DateTime.Now.ToString();
2stamp=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()方法
在这里,稍微实现了代码
注:我自己在实现作者写的代码时,有错误发生,所以经过自己调试之后,成功运行,以下为调试后的代码:
using System.IO;
namespace ApiTestExample
{
class Program
{
static void Main(string[] args)
{
FileStream ifs = null;
StreamReader sr = null;
StreamWriter sw = null;
FileStream fs = null;
try
{
ifs = new FileStream("E:\\TestCase.txt", FileMode.Open);
sr = new StreamReader(ifs);
//string stamp = DateTime.Now.Day.ToString();
string stamp = DateTime.Now.ToString();
stamp = stamp.Replace(":", "-");
sw = new StreamWriter(fs);
string line, caseID, method;
string[] tokens, tempInput;
string expected;
double actual = 0.0;
int numPass = 0, numFail = 0;
Console.WriteLine("\nCaseID Result Method Details");
Console.WriteLine("===============================");
while ((line = sr.ReadLine()) != null)
{
//Console.WriteLine(line);
tokens = line.Split(':');
caseID = tokens[0];
// Console.WriteLine(caseID);
method = tokens[1];
Console.WriteLine(method);
tempInput = tokens[2].Split(' ');
Console.WriteLine("tokens[2]=" + tokens[2]);
Console.WriteLine(tempInput.Length);
Console.WriteLine("tempInput[0]=" + tempInput[0]);
Console.WriteLine("tempInput[1]=" + tempInput[1]);
Console.WriteLine(expected);
int[] input = new int[tempInput.Length];
Console.WriteLine(input);
for (int i = 0; i < input.Length; i++)
{
input[i] = int.Parse(tempInput[i]);
Console.WriteLine(input[i]);
}
if (method == "ArihthmeticMean")
{
actual = ArihthmeticMean(input[0], input[1]);
Console.WriteLine("input[0]" + input[0]);
Console.WriteLine("input[1]" + input[1]);
Console.WriteLine("actual:" + actual);
if (actual.ToString() == expected)
{
Console.WriteLine(caseID + " Pass " + method + " actual=" + actual.ToString());
//记录测试
sw.WriteLine(caseID + " Pass " + method + " actual=" + actual.ToString());
++numPass;
}
else
{
Console.WriteLine(caseID + " Fail " + method + " actual=" + actual.ToString());
//记录测试
sw.WriteLine(caseID + " Fail " + method + " actual=" + actual.ToString());
++numFail;
}
}
else
{
Console.WriteLine(caseID + " " + method + "not yet implement");
sw.WriteLine(caseID + " " + method + "not yet implement");
}
}
Console.WriteLine("===========================end test run===============");
Console.WriteLine("pass=" + numPass + "Fail=" + numFail);
}
catch (Exception ex)
{
Console.WriteLine("Fatal error:" + ex.Message);
}
finally
{
//注意打开的文件资源一定要关闭,还有注意顺序
ifs.Close();
sr.Close();
sw.Close();
fs.Close();
}
//待测方法ArihthmeticMean
public static double ArihthmeticMean(double i, double j)
{
double result = 0.0;
result = Math.Max(i, j);
return result;
}
}
}
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自动化测试介绍基本完毕,请多多指教,谢谢!
TAG:
我的栏目
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 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 |
我的存档
数据统计
- 访问量: 39350
- 日志数: 72
- 建立时间: 2011-04-13
- 更新时间: 2013-11-13