net自动化测试之道API测试-用空串测试
2011-08-14 23:03
344 查看
Dealing with Empty String Input Arguments
You want to test empty string arguments passedto API methods under test.
Design
Use a special string token to represent anempty string in your test case file and then add
branching logic to your test harness thatpasses a true empty string argument to the API
method under test.
0001:SubString:put:computer:true
0002:SubString:xyz:computer:false
0003:SubString:emptystring:computer:true
在测试套件中增加特殊的逻辑处理有“emptystring”标记的测试用例:
tokens=line.Split(':');
if(tokens[2]=="emptystring")//specialinput
arg1="";
else
arg1=tokens[2];
boolactual=StringLib.Methods.SubString(arg1,tokens[3]);
if(actual==bool.Parse(tokens[4]))
Console.WriteLine("Pass");
else
Console.WriteLine("*FAIL*");
Comments
When testing API methods that accept stringarguments,you should always test for empty
strings.One way to deal with this is tostore a special string token such as“emptystring”in
your test case data file and then branchyour test case logic when that token is read.Suppose,
for example,you are testing a customStringLib library containing a custom SubString()
method that accepts two string argumentsand returns true if the first argument is contained
within the second argument.By design,thecustom SubString()method returns true if an
empty string is passed to its firstparameter.
Unlike null input,it is possible toindirectly store empty string input in a test case data
file.For example,the test case data string
0003:SubString::computer:true
when parsed byString.Split()into a string array named tokens will store an empty string
into tokens[2]becauseof the two consecutive colon characters.However,in general,it’s
much better to store a special string tokenbecause it makes your test case data easier to read
and validate programmatically.
The technique of embedding special stringtokens in your test case data file to deal with
empty string input can be used to test forother unusual input too.For example,suppose you
are testing a method that accepts acharacter input argument.You will want to test for control
characters such as<CR>and<LF>,andASCII vs.Unicode characters.You can store strings like
“<cr>”,“<lf>”,and“\u0041”in your test case data and then add special logic to your harness
to deal with them:
char input;
if(tokens[2]=="<CR>")//specialinput
input='\x000d';
else
input=char.Parse(tokens[2]);
If you have a lot of special tokens in yourtest case data file as is often the case,you can keep
your harness code cleaner and more scalableby writing a helper method Map(),which converts
the input value read from the test casedata file into the appropriate value.For example,you
could write:
private static charMap(string token)
{
if(token=="<CR>")
return'\x000d';
elseif(token=="<LF>")
return'\x000a';
//etc.
else
return char.Parse(token);
}
and then use it in your harness like this:
char input=Map(tokens[2]);
不像将null输入那样,在测试用例文件中可能不直接存储特殊标记表示空串。例如:
0003:SubString::computer:true
当使用String.Split()分解测试用例的时候,tokens字符串数组将空串存储在tokens[2]中,因为有两个连续的冒号。但是存储特殊字符串标记更好,因为这样我们的测试用例更容易读和使用程序验证。
在测试用例文件中签入特殊的字符串标记处理空串输入的技巧同样可以用于输入是其他特殊字符的情形。例如,假设我们测试一个接收字符输入的方法。我们想用控制字符如<CR>、<LF>、ASCII码、和Unicode字符。我们的测试用例可以用这些字符“<cr>”,“<lf>”,和 “\u0041”表示,然后在测试套件中的特殊逻辑中处理:
char input;
if(tokens[2]=="<CR>")//specialinput
input='\x000d';
else
input=char.Parse(tokens[2]);
如果我们的测试用例文件中的特殊标记像测试用例那样多,可以通过写一个Map()辅助方法使我们的测试套件的代码简洁并可扩展,该方法将从测试用例文件中读取的输入转换为方法需要的值。例如:
private static char Map(string token)
{
if(token=="<CR>")
return'\x000d';
else if(token=="<LF>")
return'\x000a';
//etc.
else
return char.Parse(token);
}
然后我们在测试套件中可以这样用:
char input=Map(tokens[2]);
参数为空串时的处理方法
ProblemYou want to test empty string arguments passedto API methods under test.
问题
如何测试将空串传给被测方法?Design
Use a special string token to represent anempty string in your test case file and then add
branching logic to your test harness thatpasses a true empty string argument to the API
method under test.
设计
在测试用例文件中,用特殊的标记字符串代表空串,然后在测试套件中增加逻辑处理传递真正的空串给被测方法。解决方案
创建如下测试用例:0001:SubString:put:computer:true
0002:SubString:xyz:computer:false
0003:SubString:emptystring:computer:true
在测试套件中增加特殊的逻辑处理有“emptystring”标记的测试用例:
tokens=line.Split(':');
if(tokens[2]=="emptystring")//specialinput
arg1="";
else
arg1=tokens[2];
boolactual=StringLib.Methods.SubString(arg1,tokens[3]);
if(actual==bool.Parse(tokens[4]))
Console.WriteLine("Pass");
else
Console.WriteLine("*FAIL*");
Comments
When testing API methods that accept stringarguments,you should always test for empty
strings.One way to deal with this is tostore a special string token such as“emptystring”in
your test case data file and then branchyour test case logic when that token is read.Suppose,
for example,you are testing a customStringLib library containing a custom SubString()
method that accepts two string argumentsand returns true if the first argument is contained
within the second argument.By design,thecustom SubString()method returns true if an
empty string is passed to its firstparameter.
Unlike null input,it is possible toindirectly store empty string input in a test case data
file.For example,the test case data string
0003:SubString::computer:true
when parsed byString.Split()into a string array named tokens will store an empty string
into tokens[2]becauseof the two consecutive colon characters.However,in general,it’s
much better to store a special string tokenbecause it makes your test case data easier to read
and validate programmatically.
The technique of embedding special stringtokens in your test case data file to deal with
empty string input can be used to test forother unusual input too.For example,suppose you
are testing a method that accepts acharacter input argument.You will want to test for control
characters such as<CR>and<LF>,andASCII vs.Unicode characters.You can store strings like
“<cr>”,“<lf>”,and“\u0041”in your test case data and then add special logic to your harness
to deal with them:
char input;
if(tokens[2]=="<CR>")//specialinput
input='\x000d';
else
input=char.Parse(tokens[2]);
If you have a lot of special tokens in yourtest case data file as is often the case,you can keep
your harness code cleaner and more scalableby writing a helper method Map(),which converts
the input value read from the test casedata file into the appropriate value.For example,you
could write:
private static charMap(string token)
{
if(token=="<CR>")
return'\x000d';
elseif(token=="<LF>")
return'\x000a';
//etc.
else
return char.Parse(token);
}
and then use it in your harness like this:
char input=Map(tokens[2]);
注解
当测试接受字符串参数的API方法的时候,我们应该总是会用空串去测试。一种方式是在测试用例文件中存储特殊的字符串标记例如“emptystring”,然后当改标记被读取的时候加以处理。假设我们正在测试一个叫StringLib的类,该类包含一个叫SubString的方法接收两个字符串参数,并且如果第二个参数包括第一个参数则返回true。通过设计,SubString方法的第一个参数是空串的时候返回true。不像将null输入那样,在测试用例文件中可能不直接存储特殊标记表示空串。例如:
0003:SubString::computer:true
当使用String.Split()分解测试用例的时候,tokens字符串数组将空串存储在tokens[2]中,因为有两个连续的冒号。但是存储特殊字符串标记更好,因为这样我们的测试用例更容易读和使用程序验证。
在测试用例文件中签入特殊的字符串标记处理空串输入的技巧同样可以用于输入是其他特殊字符的情形。例如,假设我们测试一个接收字符输入的方法。我们想用控制字符如<CR>、<LF>、ASCII码、和Unicode字符。我们的测试用例可以用这些字符“<cr>”,“<lf>”,和 “\u0041”表示,然后在测试套件中的特殊逻辑中处理:
char input;
if(tokens[2]=="<CR>")//specialinput
input='\x000d';
else
input=char.Parse(tokens[2]);
如果我们的测试用例文件中的特殊标记像测试用例那样多,可以通过写一个Map()辅助方法使我们的测试套件的代码简洁并可扩展,该方法将从测试用例文件中读取的输入转换为方法需要的值。例如:
private static char Map(string token)
{
if(token=="<CR>")
return'\x000d';
else if(token=="<LF>")
return'\x000a';
//etc.
else
return char.Parse(token);
}
然后我们在测试套件中可以这样用:
char input=Map(tokens[2]);
相关文章推荐
- net自动化测试之道API测试-分解一个测试用例
- net自动化测试之道API测试-确定测试结果
- net自动化测试之道API测试-确定测试套件运行的总时间
- net自动化测试之道API测试-处理输入为null/期望结果为null的情况
- net自动化测试之道API测试-测试抛出异常的方法
- net自动化测试之道API测试-自动发送Email报告测试失败
- net自动化测试之道API测试-用时间戳区别测试结果
- net自动化测试之道API测试-自动运行测试套件
- net自动化测试之道API测试-统计测试结果
- net自动化测试之道API测试-引言
- net自动化测试之道API测试-例子程序:ApiTest
- net自动化测试之道API测试-存储测试用例
- net自动化测试之道API测试-读取测试用例
- net自动化测试之道API测试-数据转换
- 利用BenchmarkDotNet 测试 .Net Core API 同步和异步方法性能
- .NET自动化测试---API轻量级测试
- 使用 Swagger 自动生成 ASP.NET Core Web API 的文档、在线帮助测试文档(ASP.NET Core Web API 自动生成文档)
- 使用 Swagger 自动生成 ASP.NET Core Web API 的文档、在线帮助测试文档(ASP.NET Core Web API 自动生成文档)
- 如何在ASP.NET Core Web API测试中使用Postman
- Ubuntu Server 16.04下ASP.NET Core Web Api + MySql + Dapper在 Jexus、nginx 下的简单测试