示例程序:ApiTest
此程序综合了前15篇的多种技术,编写了一个完整的轻量级的API测试套件。待测方法包括:ArithmeticMean(),GeometricMean()和HarmonicMean(),它们在API开始时候描述过。轻量级测试套件完整代码如下,这个程序从TestCases.txt文件每次读入一行测试用例数据。然后测试套件会解析出测试用例ID,并且一并得到待测方法的名称、输入值和期望值。输入值为传给待测方法,接下来程序把得到的实际结果和测试用例的期望结果进行比较 。测试通过与否,最终的结果会发送到命令行程序并且记录到TestResults.txt文件
==============Program ApiTest====================
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using MathLib;
namespace TestAutomation
{
class Program
{
[STAThread]
static void Main(string[] args)
{
try
{
FileStream ifs = new FileStream("..//..//TestCases.txt", FileMode.Open);
StreamReader sr = new StreamReader(ifs);
string stamp = 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 Result Method 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");
}
}//测试用例循环
Console.WriteLine("/n==================end test run =================");
Console.WriteLine("/nPass = " + numPass + "Fail = " + numFail);
sw.WriteLine("/nPass = " + numPass + "Fail = " + numFail);
sr.Close();
ifs.Close();
sw.Close();
ofs.Close();
}
catch (Exception ex)
{
Console.WriteLine("Fatal error:" + ex.Message);
}
Console.ReadLine();
}//Main()
}//Class Program
}//ns TestAutomation
联系客服