您的位置:首页 > 其它

net自动化测试之道API测试-分解一个测试用例

2011-08-10 21:38 281 查看
Parsing a Test Case

分解一个测试用例

Problem

You want to parse the individual fields ofa character-delimited test case.

问题

如何获得字符分隔的测试用例的单个字段呢?

Design

Use the String.Split()method,passingas the input argument the delimiting character and

storing the return value into a stringarray.

设计

使用String.Split() 方法,将分隔字符作为输入参数传入,并且将返回值存储到一个字符数组中。

解决方案

string line,caseID,method;

string[]tokens,tempInput;

string expected;

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

{

tokens=line.Split(':');

caseID=tokens[0];

method=tokens[1];

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

expected=tokens[3];

//etc.

}

After reading a line of test case data intoa string variable line,calling the Split()method with

the colon character passed in as anargument will break the line into the parts between the

colons.These substrings are assigned to thestring array tokens.So,tokens[0]will hold the

first field,which is the test case ID(forexample“001”),tokens[1]will hold the string identify-

ing the method under test(for example“ArithmeticMean”),tokens[2]willhold the input

vector as a string(for example“24 8”),and tokens[3]will hold the expected value(for exam-

ple“4.667”).Next,youcall the Split()method using a blank space argument on tokens[2]

and assign the result to the string arraytempInput.If tokens[2]has“2 4 8”,then tempInput[0]

will hold“2”,tempInput[1]will hold“4”,andtempInput[2]will hold“8”.

注解

读取一行测试用例数据,存储在一个字符串变量中,然后调用Split()方法,将冒号作为参数传入,这样将一行测试用例以冒号为界分割成若干部分。这些子字符串被赋给字符串数组tokens。因此,tokens[0]将存储第一个字段,也就是测试用例ID(例如“001”),tokens[1]将存储被测方法的名称(例如ArithmeticMean),tokens[2]将存储输入序列(例如2 4 8),tokens[3]将存储预期结果(例如4.667)。接下来我们对tokens[2]调用Split()方法,以空格作为参数,并且赋给字符串数组tempInput。如果tokens[2]={2
4 8},则tempInput[0]=”2”,tempInput[1]=”4”, tempInput[2]=”8。”

If you need to use more than one separatorcharacter,you can create a character array

containing the separators and then passthat array to Split().For example,

char[]separators=newchar[]{'#',':','!'};

string[]parts=line.Split(separators);

will break the string variable line intopieces wherever there is a pound sign,colon,or exclama-

tion point character and assign thosesubstrings to the string array parts.

如果我们需要使用多个分隔字符,可以创建一个字符数组,包含我们需要的分隔字符,然后将该数组传递给Split()。例如:

char[]separators=new char[]{'#',':','!'};

string[]parts=line.Split(separators);

这样无论是”#”,”:”,还是”!”分隔符,一行字符串变量将被分隔开若干个子串,并且这些子串赋给字符串数组parts。

The Split()method will satisfy most of yoursimple text-parsing needs for lightweight test-

automation situations.A significantalternative to using Split()is to use regular expressions.

One advantage of using regular expressionsis that they are more powerful,in the sense that you

can get a lot of parsing done in very fewlines of code.One disadvantage of regular expressions is

that they are harder to understand by thosewho do not use them often because the syntax is rel-

atively unusual compared with mostC#programming constructs.

在轻量级自动测试场景中,Split()方法能满足大部分简单的文本分离需要。一个比使用Split()更好的方式使用正则表达式。使用正则表达式的一个优点是可以使用简洁的代码做更多的分割工作,它的一个缺点是对于那些不太常使用正则表达式的人来说它太难,因为相比C#它的语法相对不寻常。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: