net自动化测试之道API测试-分解一个测试用例
2011-08-10 21:38
281 查看
Parsing a Test Case
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[]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”.
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#它的语法相对不寻常。
分解一个测试用例
ProblemYou 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]={24 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#它的语法相对不寻常。
相关文章推荐
- net自动化测试之道API测试-存储测试用例
- net自动化测试之道API测试-读取测试用例
- net自动化测试之道API测试-用空串测试
- net自动化测试之道API测试-用时间戳区别测试结果
- net自动化测试之道API测试-自动发送Email报告测试失败
- net自动化测试之道API测试-统计测试结果
- net自动化测试之道API测试-自动运行测试套件
- net自动化测试之道API测试-数据转换
- net自动化测试之道API测试-例子程序:ApiTest
- net自动化测试之道API测试-引言
- Python自动化测试例子--一个简单的自动化测试用例脚本--批量执行测试用例
- net自动化测试之道API测试-确定测试结果
- 转载:用一个小例子来说明手工测试,自动化测试,系统命令,编程语言,API的关系
- ruby+selenium-webdriver一步一步完成自动化测试(2)—–一个测试用例
- ruby+selenium-webdriver一步一步完成自动化测试-----一个测试用例
- net自动化测试之道API测试-确定测试套件运行的总时间
- net自动化测试之道API测试-处理输入为null/期望结果为null的情况
- net自动化测试之道API测试-测试抛出异常的方法
- 一个纸水杯的测试用例设计
- 一个用TestDirector生成的测试用例