net自动化测试之道API测试-数据转换

Converting Data to an Appropriate DataType

数据转换

Problem

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

解决方案

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

 

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类型的参数,这也正是我们想要的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值