net自动化测试之道API测试-数据转换
2011-08-11 22:34
501 查看
Converting Data to an Appropriate DataType
You want to convert your test case inputdata or expected result from type string into some
other data type,so you can pass the data tothe method under test or compare the expected
result with an actual result.
Design
Perform an explicit type conversion withthe appropriate static Parse()method.
设计
使用合适的静态方法Parse()执行显示类型转换。
Solution
for(int i=0;i<input.Length;++i)
input[i]=int.Parse(tempInput[i]);
Comments
If you store your test case data in a textfile and then parse the test case inputs,you will end up
with type string.If the method under testaccepts any data type other than string you need to
convert the inputs.In the precedingsolution,if the string array tempInput holds{“2”,”4”,”8”}
then you first create an integer arraynamed input with the same size as tempInput.After the
loop executes,input[0]will hold 2(as aninteger),input[1]will hold 4,and input[2]will hold 8.
Including type string,the C#language has 14data types that you’ll deal with most often as
listed in Table 1-1.
Table 1-1.Common C#Data Types andCorresponding.NET Types
Each of these C#data types supports astatic Parse()method that accepts a string argument and returns the callingdata type.For example,
string s1="345.67";
double d=double.Parse(s1);
string s2="true";
bool b=bool.Parse(s2);
will assign numeric 345.67 to variable dand logical true to b.An alternative to using Parse()is
to use static methods in the System.Convertclass.For instance, string s1="345.67";
double d=Convert.ToDouble(s1);
string s2="true";
bool b=Convert.ToBoolean(s2);
is equivalent to the precedingParse()examples.The Convert methods transform to and from
.NET data types(such asInt32)rather than directly to their C#counterparts(such as int).One
advantage of using Convert isthat it is not syntactically C#-centric like Parse()is,so if you
ever recast your automationfrom C#to VB.NET you’ll have less work to do.Advantages of
using the Parse()methodinclude the fact that it maps directly to C#data types,which makes
your code somewhat easier toread if you are in a 100%C#environment.In addition,Parse()
is more specific than theConvert methods,because it accepts only type string as a parameter
(which is exactly what youneed when dealing with test case data stored in a text file).
每一个C#数据类型都支持接收一个string类型参数的静态方法Parse()并且返回调用的数据类型。例如
string s1="345.67";
double d=double.Parse(s1);
string s2="true";
bool b=bool.Parse(s2);
上面的语句的结果是数字345.67将会被赋给d,逻辑true赋给b。一种替代使用Parse()的方式是使用System.Convert类中的静态方法,例如:
string s1="345.67";
double d=Convert.ToDouble(s1);
string s2="true";
bool b=Convert.ToBoolean(s2);
上面的语句将获得和之前使用Parse()一样的结果。使用Convert方法是转化为.NET的数据类型(如Int32)而不是直接转为C#的与.NET数据类型相当的类型(如int)。Convert方法不是以 C#为中心的语法,而Parse方法是的,使用Convert方法的一个优点是如果我们想将自动化程序由C#转化为VB.NET,它的工作量将少些。使用Parse方法的优点是在100%C#环境中,它的可读性要比使用Convert方法相对强。另外,在测试用例存储在文本文件中的情形下,Parse方法比Convert方法更明确,因为它只接受string类型的参数,这也正是我们想要的。
数据转换
ProblemYou want to convert your test case inputdata or expected result from type string into some
other data type,so you can pass the data tothe method under test or compare the expected
result with an actual result.
问题
我们分解的测试用例数据和期望结果是字符串类型的,但是被测方法的参数和返回值确不是字符串类型的,因此需要转化数据类型,使测试用例的输入与被测方法参数的类型一致,期望结果与实际结果之间可比较,那如何进行数据转换呢?Design
Perform an explicit type conversion withthe appropriate static Parse()method.
设计
使用合适的静态方法Parse()执行显示类型转换。
Solution
解决方案
int[]input=new int[tempInput.Length];for(int i=0;i<input.Length;++i)
input[i]=int.Parse(tempInput[i]);
Comments
If you store your test case data in a textfile and then parse the test case inputs,you will end up
with type string.If the method under testaccepts any data type other than string you need to
convert the inputs.In the precedingsolution,if the string array tempInput holds{“2”,”4”,”8”}
then you first create an integer arraynamed input with the same size as tempInput.After the
loop executes,input[0]will hold 2(as aninteger),input[1]will hold 4,and input[2]will hold 8.
Including type string,the C#language has 14data types that you’ll deal with most often as
listed in Table 1-1.
注解
如果我们的测试用例存储在一个文本文件中,并且已经将测试用例的输入分解出来了,此时,我们获得的是字符串类型的值。如果被测方法接受的不是字符串类型,而是其他类型的值,则我们需要对输入值进行转换。在上面的解决方案中,假设string类型的数组tempInput ={“2”,”4”,”8”},那么我们首先要创建一个与tempInput大小一样的integer类型的数组input。循环执行后,input[0]=2, input[1]=4,input[2]=8。表1-1中的数据类型是C#语言中,我们将经常使用的数据类型,包括string类型。Table 1-1.Common C#Data Types andCorresponding.NET Types
C#Type | Corresponding.NET Type |
int | Int32 |
short | Int16 |
long | Int64 |
uint | Uint32 |
ushort | Uint16 |
ulong | Uint64 |
byte | Byte |
sbyte | Sbyte |
char | Char |
bool | Boolean |
float | Single |
double | Double |
decimal | Decimal |
string s1="345.67";
double d=double.Parse(s1);
string s2="true";
bool b=bool.Parse(s2);
will assign numeric 345.67 to variable dand logical true to b.An alternative to using Parse()is
to use static methods in the System.Convertclass.For instance, string s1="345.67";
double d=Convert.ToDouble(s1);
string s2="true";
bool b=Convert.ToBoolean(s2);
is equivalent to the precedingParse()examples.The Convert methods transform to and from
.NET data types(such asInt32)rather than directly to their C#counterparts(such as int).One
advantage of using Convert isthat it is not syntactically C#-centric like Parse()is,so if you
ever recast your automationfrom C#to VB.NET you’ll have less work to do.Advantages of
using the Parse()methodinclude the fact that it maps directly to C#data types,which makes
your code somewhat easier toread if you are in a 100%C#environment.In addition,Parse()
is more specific than theConvert methods,because it accepts only type string as a parameter
(which is exactly what youneed when dealing with test case data stored in a text file).
每一个C#数据类型都支持接收一个string类型参数的静态方法Parse()并且返回调用的数据类型。例如
string s1="345.67";
double d=double.Parse(s1);
string s2="true";
bool b=bool.Parse(s2);
上面的语句的结果是数字345.67将会被赋给d,逻辑true赋给b。一种替代使用Parse()的方式是使用System.Convert类中的静态方法,例如:
string s1="345.67";
double d=Convert.ToDouble(s1);
string s2="true";
bool b=Convert.ToBoolean(s2);
上面的语句将获得和之前使用Parse()一样的结果。使用Convert方法是转化为.NET的数据类型(如Int32)而不是直接转为C#的与.NET数据类型相当的类型(如int)。Convert方法不是以 C#为中心的语法,而Parse方法是的,使用Convert方法的一个优点是如果我们想将自动化程序由C#转化为VB.NET,它的工作量将少些。使用Parse方法的优点是在100%C#环境中,它的可读性要比使用Convert方法相对强。另外,在测试用例存储在文本文件中的情形下,Parse方法比Convert方法更明确,因为它只接受string类型的参数,这也正是我们想要的。
相关文章推荐
- net自动化测试之道API测试-分解一个测试用例
- net自动化测试之道API测试-确定测试套件运行的总时间
- net自动化测试之道API测试-处理输入为null/期望结果为null的情况
- net自动化测试之道API测试-测试抛出异常的方法
- net自动化测试之道API测试-用空串测试
- net自动化测试之道API测试-用时间戳区别测试结果
- net自动化测试之道API测试-自动发送Email报告测试失败
- net自动化测试之道API测试-统计测试结果
- net自动化测试之道API测试-自动运行测试套件
- net自动化测试之道API测试-引言
- net自动化测试之道API测试-例子程序:ApiTest
- net自动化测试之道API测试-存储测试用例
- net自动化测试之道API测试-读取测试用例
- net自动化测试之道API测试-确定测试结果
- Ubuntu Server 16.04下ASP.NET Core Web Api + MySql + Dapper在 Jexus、nginx 下的简单测试
- 利用BenchmarkDotNet 测试 .Net Core API 同步和异步方法性能
- .NET自动化测试---API轻量级测试
- 如何在ASP.NET Core Web API测试中使用Postman
- 自动化测试 : 确保界面用SendMessage就可以自动化测试, 不需要特别的键盘鼠标API
- 使用 Swagger 自动生成 ASP.NET Core Web API 的文档、在线帮助测试文档(ASP.NET Core Web API 自动生成文档)