上篇博客介绍了使用XML存储测试用例,流处理模式的自动化测试程序的编写,这篇将介绍使用XML存储测试用例,缓存处理模式的自动化测试程序的编写。即先将所有测试用例读取出来放到一个ArrayList中,执行的时候从这个ArrayList中拿测试用例,并把测试结果统一放到另一个ArrayList中,最后将第二个ArrayList中的测试结果保存到外部XML文件中。
参考上篇博客
1、 准备被测应用程序
2、 准备XML格式的测试用例
3、 准备TestCase类和TestCaseResult类
下面就开始自动化测试套件的设计了。
上篇博客代码写得有点乱,没做任何整理。其实任何测试的执行都会包括读取测试用例、执行测试、保存测试结果这几个步骤。因此在此例子中我会将代码封装到方法中。
4、 编写自动化测试代码
上篇博客好像忘记说要添加被测应用程序的引用了。
在上篇博客创建的那个TestHarnessDesignPartterns项目下创建一个XMLBufferedHarness类。
该类中封装了三个方法,ReadData()方法读取测试用例,RunTests()方法执行测试,SaveResults()方法将测试结果存储到外部XML中。我通过该类的构造函数将测试用例文件和测试结果文件传递给该类。即该类有个这样签名的构造函数:
public XMLBufferedHarness(string testCaseFile,string testResultFile)
XMLBufferedHarness类的全部代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using MathLib;
using System.Collections;
using TestHarnessDesignPartterns;
namespace TestHarnessDesignPartterns
{
class XMLBufferedHarness
{
private XmlTextReader xtr = null;
private XmlTextWriter xtw = null;
private ArrayList tcd = null;//test case data
private ArrayList tcr = null;//test case results
string id, input, expected, actual;
TestCase tc = null;
TestCaseResult r = null;
public XMLBufferedHarness(string testCaseFile,string testResultFile)
{
xtr = new XmlTextReader(testCaseFile);
xtw = new XmlTextWriter(testResultFile, System.Text.Encoding.UTF8);
xtr.WhitespaceHandling = WhitespaceHandling.None;
xtw.Formatting = Formatting.Indented;
tcd = new ArrayList();
tcr = new ArrayList();
}
public void ReadData()
{
//use xtr to read datafile into tcd here
while (!xtr.EOF)//main loop
{
if (xtr.Name == "testcases" && !xtr.IsStartElement()) break;
while (xtr.Name != "case" || !xtr.IsStartElement())
xtr.Read();//advance to a<case>element if not there yet
id = xtr.GetAttribute("id");
xtr.Read();//advance to<input>
input = xtr.ReadElementString("input");//advance to<expected>
expected = xtr.ReadElementString("expected");//advance to</case>
tc = new TestCase(id, input, expected);
tcd.Add(tc);
xtr.Read();//advance to next<case>or</TestResults>
}
xtr.Close();
}
public void RunTests()
{
//run tests,store results to tcr here
for(int i=0;i<tcd.Count;++i)
{
tc=(TestCase)tcd[i];
string[] inputA = tc.input.Split(' ');
int [] inputs=new int[inputA.Length];
for (int j = 0; j < inputA.Length; ++j)
inputs[j] = int.Parse(inputA[j]);
actual = Methods.ArithmeticMean(inputs).ToString("F4");
if (actual == tc.expected)
r = new TestCaseResult(tc.id, tc.input, tc.expected, actual, "Pass");
else
r = new TestCaseResult(tc.id, tc.input, tc.expected, actual, "*FAIL*");
tcr.Add(r);
}//main processing loop
}
public void SaveResults()
{
//save results to resultfile here
xtw.WriteStartDocument();
xtw.WriteStartElement("TestResults");//root node
for (int i = 0; i < tcr.Count; ++i)
{
r = (TestCaseResult)tcr[i];
xtw.WriteStartElement("case");
xtw.WriteStartAttribute("id", null);
xtw.WriteString(r.id); xtw.WriteEndAttribute();
xtw.WriteStartElement("input");
xtw.WriteString(r.input); xtw.WriteEndElement();
xtw.WriteStartElement("expected");
xtw.WriteString(r.expected); xtw.WriteEndElement();
xtw.WriteStartElement("actual"); xtw.WriteString(r.actual);
xtw.WriteEndElement();
xtw.WriteStartElement("result");
xtw.WriteString(r.result); xtw.WriteEndElement();
xtw.WriteEndElement();//</case>
}
xtw.WriteEndElement();//</TestResults>
xtw.Close();
Console.WriteLine("\nEnd test run\n");
}
}
}
当然该类封装的并不是很好,通用性不强,不是很好的面向对象的设计。如果是设计自动化测试框架,读取测试用例、执行测试、存储测试结果这几个步骤应该可以更抽象。
好了,先不管这么多,我们在main函数中调用下:
string stamp = DateTime.Now.ToString("s");
stamp = stamp.Replace(":", "-");
string testCaseFile = "..\\..\\..\\..\\TestCase\\DesignPatternTestCases.xml";
string testResultFile = "..\\..\\..\\..\\TestResult\\XMLFileStreamingResults-"+stamp+".xml";
try
{
XMLBufferedHarness h1 = new XMLBufferedHarness(testCaseFile, testResultFile);
h1.ReadData();
h1.RunTests();
h1.SaveResults();
}
catch (Exception ex)
{
Console.WriteLine("Fatal error:" + ex.Message);
}
运行下吧,看看效果。