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類型的參數,這也正是我們想要的。

發佈了10 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章