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.
和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故意设计了一个错误的预期结果,用来验证测试套件的逻辑。
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故意设计了一个错误的预期结果,用来验证测试套件的逻辑。
相关文章推荐
- net自动化测试之道基于反射的UI自动化测试—例子程序:ReflectionUITest
- net自动化测试之道API测试-引言
- VB.Net程序代码:Hashtable与ListView结合使用测试例子
- 转载:用一个小例子来说明手工测试,自动化测试,系统命令,编程语言,API的关系
- net自动化测试之道API测试-确定测试套件运行的总时间
- 用VS.NET中的测试工具(Application Center Test)测试ASP.NET程序
- net自动化测试之道API测试-存储测试用例
- net自动化测试之道API测试-处理输入为null/期望结果为null的情况
- net自动化测试之道API测试-读取测试用例
- net自动化测试之道API测试-测试抛出异常的方法
- net自动化测试之道API测试-分解一个测试用例
- net自动化测试之道API测试-用空串测试
- net自动化测试之道API测试-用时间戳区别测试结果
- net自动化测试之道API测试-自动发送Email报告测试失败
- net自动化测试之道API测试-数据转换
- net自动化测试之道API测试-统计测试结果
- net自动化测试之道API测试-自动运行测试套件
- net自动化测试之道API测试-确定测试结果
- c# asp.net 测试程序执行时间
- C# 利用VS2010结核TestDriven.Net工具进行代码测试