您的位置:首页 > 其它

net自动化测试之道API测试-例子程序:ApiTest

2011-08-15 22:47 567 查看
Example Program:ApiTest

This program combines many of thetechniques we’ve seen in this chapter into a complete

lightweight API test harness.The methodsunder test are ArithmeticMean(),GeometricMean(),

and HarmonicMean()as described at the beginning of this chapter.The completelightweight test

harness listing is shown in Listing 1-1.Theprogram reads test case data a line at a time from

the file TestCases.txt.Then the harnessparses the test case ID,which includes the method to

test,input values,and an expectedresult.Input values are sent to the method under test,and

an actual result value is obtained andcompared with the test case expected result value.A pass

or fail result is sent to the command shelland logged to the file TestResults.txt.

例子程序:ApiTest

这个例子程序是一个完整的轻量级API测试套件,该例子应用了我们在本章介绍的的许多技巧,使用的被测方法是在本章开头描述的那几个方法:ArithmeticMean(),GeometricMean(),

和HarmonicMean()。在例子中,程序从TestCases.txt文件中一行一行的读取测试用例,然后分解每个测试用例的ID、输入值和期望结果。接着程序将输入值传给被测方法,获取方法的返回值,与期望结果比较。然后将通过或失败结果显示到终端并且记录到TestResults.txt文件中。程序代码如下:

using System;

using System.IO;

using MathLib;//houses methods under test

namespace TestAutomation

{

class Class1

{

[STAThread]

static void Main(string[]args)

{

try

{

FileStream ifs=newFileStream("..\\..\\TestCases.txt",

FileMode.Open);

StreamReader sr=new StreamReader(ifs);

stringstamp=DateTime.Now.ToString("s");

stamp=stamp.Replace(":","-");

FileStream ofs=new FileStream("..\\..\\TestResults"+

stamp+".txt",FileMode.CreateNew);

StreamWriter sw=new StreamWriter(ofs);

string line,caseID,method;

string[]tokens,tempInput;

string expected;

double actual=0.0;

int numPass=0,numFail=0;

Console.WriteLine("\nCaseID ResultMethod Details");

Console.WriteLine("================================\n");

while((line=sr.ReadLine())!=null)

{

tokens=line.Split(':');

caseID=tokens[0];

method=tokens[1];

tempInput=tokens[2].Split('');

expected=tokens[3];

int[]input=new int[tempInput.Length];

for(int i=0;i<input.Length;++i)

input[i]=int.Parse(tempInput[i]);

if(method=="ArithmeticMean")

{

actual=MathLib.Methods.ArithmeticMean(input);

if(actual.ToString("F4")==expected)

{

Console.WriteLine(caseID+"Pass"+method+

"actual="+actual.ToString("F4"));

sw.WriteLine(caseID+"Pass"+method+

"actual="+actual.ToString("F4"));

++numPass;

}

Else

{

Console.WriteLine(caseID+"*FAIL*"+method+

"actual="+actual.ToString("F4")+

"expected="+expected);

sw.WriteLine(caseID+"*FAIL*"+method+

"actual="+actual.ToString("F4")+

"expected="+expected);

++numFail;

}

}

else if(method=="GeometricMean")

{

MathLib.Methods m=new MathLib.Methods();

actual=m.GeometricMean(input);

if(actual.ToString("F4")==expected)

{

Console.WriteLine(caseID+"Pass"+method+

"actual="+actual.ToString("F4"));

sw.WriteLine(caseID+"Pass"+method+

"actual="+actual.ToString("F4"));

++numPass;

}

else

{

Console.WriteLine(caseID+"*FAIL*"+method+

"actual="+actual.ToString("F4")+

"expected="+expected);

sw.WriteLine(caseID+"*FAIL*"+method+

"actual="+actual.ToString("F4")+

"expected="+expected);

++numFail;

}

}

else

{

Console.WriteLine(caseID+""+method+

"Not yet implemented");

sw.WriteLine(caseID+""+method+

"Not yet implemented");

}

}//test case loop

Console.WriteLine("\n=========end testrun==========");

Console.WriteLine("\nPass="+numPass+

"Fail="+numFail);

sw.WriteLine(Environment.NewLine+"Pass="+numPass+

"Fail="+numFail);

sr.Close();

ifs.Close();

sw.Close();

ofs.Close();

}

catch(Exception ex)

{

Console.WriteLine("Fatalerror:"+ex.Message);

}

Console.ReadLine();

}//Main()

}//class Class1

}//ns TestAutomation

当TestCases.txt文件内容如下:

0001:ArithmeticMean:2 4 8:4.6667

0002:ArithmeticMean:1 5:3.0000

0003:ArithmeticMean:1 2 4 8 16 32:10.5000

0004:GeometricMean:1 2 4 8 16 32:6.6569

0005:GeometricMean:0:0.0000

0006:GeometricMean:2 4 8:4.0000

0007:HarmonicMean:2 4 8:3.4286

0008:HarmonicMean:2 3 6:3.0000

运行该文件的输出结果为:

0001 Pass ArithmeticMean actual=4.6667

0002 Pass ArithmeticMean actual=3.0000

0003 Pass ArithmeticMean actual=10.5000

0004*FAIL*GeometricMean actual=5.6569expected=6.6569

0005 Pass GeometricMean actual=0.0000

0006 Pass GeometricMean actual=4.0000

0007 HarmonicMean Not yet implemented

0008 HarmonicMean Not yet implemented

Pass=5 Fail=1

Test case 0004 has a deliberately incorrectexpected value to check the validity of the test

harness logic.

用例0004故意设计了一个错误的预期结果,用来验证测试套件的逻辑。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: