您的位置:首页 > 其它

net自动化测试之道API测试-读取测试用例

2011-08-10 21:31 337 查看
Reading Test Case Data

读取测试用例数据

Problem

You want to read each test case in a testcase file stored as a simple text file.

问题

如何从简单文本文件中读取每个测试用例呢?

Design

Iterate through each line of the test casefile using a while loop with a System.IO.StreamReader

object.

设计

使用while循环语句循环每一行,用System.IO.StreamReader对象读取。

解决方案

Solution

FileStream fs=newFileStream("..\\..\\TestCases.txt",FileMode.Open);

StreamReader sr=new StreamReader(fs);

string line;

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

{

//parse each test case line

//call method under test

//determine pass or fail

//log test case result

}

sr.Close();

fs.Close();

注解

In general,console applications,rather thanWindows-based applications,are best suited for

lightweight test automationharnesses.Console applications easily integrate into legacy test

systems and can be easily manipulated in aWindows environment.If you do design a harness

as a Windows application,make sure that itcan be fully manipulated from the command line.

通常,轻量级自动测试套件使用控制台应用程序比使用windows应用程序实现更合适。因为控制台应用程序更容易与过去的测试系统整合,在windows的环境中更容易操作。如果你设计了一个windows 应用程序的测试套件,请确保可以完全从控制台操作。

This solution assumes you have placed a usingSystem.IO;statement in your harness so

you can access the FileStream andStreamReader classes without having to fully qualify them.

We also assume that the test case data fileis named TestCases.txt and is located two directo-

ries above the test harnessexecutable.Relative paths to test case data files are generally better

than absolute paths like C:\\Here\\There\\TestCases.txt because relative pathsallow you to

move the test harness root directory andsubdirectories as a whole without breaking the har-

ness paths.However,relative paths may breakyour harness if the directory structure of your

test system changes.A good alternative isto parameterize the path and name of the test case

data file:

在这个解决方案中我们假设在套件中放置了using System.IO;语句,这样我们不用全部引用,就可以使用FileStream和StreamReader类了。我还假设存储测试用例的文件名为TestCases.txt,并且放在测试套件执行程序的上两级目录下。相对路径通常要比绝对路径(如C:\\Here\\There\\TestCases.txt)灵活,因为如果我们要将测试套件的根目录以及子目录作为整体移动,相对路径就不需要改变路径。但是,如果测试系统的目录结构变了,在相对路径下,测试套件可能会失效。一种好的方式是参数化测试用例文件的路径和名称:

static void Main(string[]args)

{

string testCaseFile=args[0];

FileStream fs=new FileStream(testCaseFile,FileMode.Open);

//etc.

}

Then you can call the harness along thelines of

然后,我们可以像下面这样调用套件:

C:\Harness\bin\Debug>Run.exe..\..\TestCases.txt

In this solution,FileStream andStreamReader objects are used.Alternatively,you can use

static methods in the System.IO.File classsuch as File.Open().If you expect that two or more

test harnesses may be accessing the testcase data file simultaneously,you can use the over-

loaded FileStream constructor that includesa FileShare parameter to specify how the file will

be shared.

在这个解决方案中,我们使用FileStream和StreamReader对象。也可以使用System.IO中的静态方法,如File.Open()。如果你想让两个或者更多的测试套件同时访问测试用例文件,你可以使用包含FileShare参数的重载的FileStream构造器,FileShare参数用来说明文件将如何被共享。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: