軟件測試中LoadRunner函數中的幾個陷阱
1、atof
在loadrunner中如果直接用
float f;
f=atof("123.00");
lr_output_message("%f",f);
輸出的結果會是1244128.00,根本不是我們想要的。
因為float,double型在不同的平臺下長度不一樣,所以在loadrunner中調用atof需要顯式的聲明這個函數。
如下:
doubleatof (const char *string);
float f;
f=atof("123.00");
lr_output_message("%.2f",f);
這樣就能輸出結果:123.00。
其實,在LR關于atof的幫助文檔描述中有提到這點,要求使用這個函數前“must be explicitly declared in Vugen scripts. ”,同樣的要求也出現在atol函數的描述中。
2、lr.save_string
在LoadRunner中,使用.NET VUser時可以使用lr.save_string來存入一個變量,但是其使用方法卻與通常使用的lr_save_string有區別,不小心的話容易“中招”。
C語言的lr_save_string的定義如下:
int lr_save_string (const char *param_value, const char *param_name);
注意:參數值在前面,參數名在后面
而.NET VUser的lr.save_string的參數使用恰好相反,參數名在前面,參數值應該放到后面。
但是LR的幫助文檔并沒有關于lr.save_string的定義,如果在腳本中選中“lr.save_string”,然后按F1,則直接蹦到lr_save_string的定義描述中,極容易誤導人!
3、ftp_put
ftp_put 是LoadRunner中的FTP函數,用于上傳文件到FTP服務器。定義如下:
int ftp_put ( char *transaction, LAST);
查看LR的幫助文檔可獲得如下例子:
// Send the file "ftp_file.txt" to the for_jon directory.
ftp_put("Ftp_Put",
"PATH=f:/ftp_file.txt", "TARGET_PATH=/pub/for_jon", "MODE=ASCII",
ENDITEM ,
LAST);
當你興匆匆地以為拿過來修改一下就可以用了的時候,LR卻提示如下錯誤:
Starting action Action.
globals.h(101): Warning message:PATH=D:/ftp.txt is not ftp_put valid option
globals.h(101): Debug message:Putting file test.txt in /Qdownload/test.txt, passive mode set to 0
globals.h(101): Error -86026:Failed to open D:\LoadRunner\LRProject\lr_FTP1\test.txt for reading.
globals.h(101): Error -86027:Failed to put data: 226 Transfer complete
Abort was called from an action.
提示錯誤是文件不能讀,但是文件明明就不在錯誤所提示的D:\LoadRunner\LRProject\lr_FTP1\test.txt 中,而是在D:/ftp.txt。
后來看到錯誤提示之前的一個Warning寫道PATH=D:/ftp.txt is not ftp_put valid option,難道錯誤是這里引起的?查看幫助文檔關于item_list的描述可知:
item_list
A list of all the items for this function. Enclose all entries with quotes.
SOURCE_PATH: The file to upload to the FTP server.
OR
MSOURCE_PATH - Like SOURCE_PATH, but using wildcards to specify multiple files. If wildcards are not specified, all the files in the MSOURCE_PATH are uploaded.
TARGET_PATH (optional) - the path and filename in which to place the file.
if (M)SOURCE_PATH is specified, but TARGET_PATH is not specified, the file is stored in the root directory of the FTP server, with the original file name.
MODE (optional) - Retrieval mode ASCII or BINARY (default).
PASSIVE (optional) - Sets the communication protocol to Passive Mode FTP. To enable, pass "PASSIVE=TRUE".
ENDITEM - Marks the end of the list. (no quotes)
原來是SOURCE_PATH而不是例子所說的PATH,好吧,等你改成SOURCE_PATH后,如下所示:
// Send the file "ftp_file.txt" to the for_jon directory.
ftp_put("Ftp_Put",
"SOURCE_PATH=f:/ftp_file.txt", "TARGET_PATH=/pub/for_jon", "MODE=ASCII",
ENDITEM ,
LAST);
興匆匆地以為就行了,一運行又報錯:
globals.h(101): Error -86025:Put failed; 550 /pub/for_jon: Not a regular file
原來這次是TARGET_PATH的問題,再看LR的幫助文檔:
TARGET_PATH (optional) - the path and filename in which to place the file.
if (M)SOURCE_PATH is specified, but TARGET_PATH is not specified, the file is stored in the root directory of the FTP server, with the original file name.
原來TARGET_PATH要包含文件名,因此改成:
// Send the file "ftp_file.txt" to the for_jon directory.
ftp_put("Ftp_Put",
"SOURCE_PATH=f:/ftp_file.txt", "TARGET_PATH=/pub/for_jon/ftp_file.txt", "MODE=ASCII",
ENDITEM ,
LAST);
這次終于行了。
同樣的,在ftp_put_ex函數中也有類似的錯誤:
In the following example, the ftp_get_ex function gets the file ftp_file.txt from the FTP server.
// Send the file "ftp_file.txt" to the for_jon directory.
ftp_put_ex(&ftp, "Ftp_Put",
"PATH=f:/ftp_file.txt", "TARGET_PATH=/pub/for_jon", "MODE=ASCII",
ENDITEM,
LAST);
而且例子代碼寫的是“ftp_put_ex”, 描述例子時卻說的是“ftp_get_ex”。
LR是非常優秀的性能測試工具,但是如果幫助文檔中類似這樣的錯誤比較多的話,對初學者而言就又多了一道學習的門檻:鑒別聯機幫助信息的真偽的能力。
文章來源于領測軟件測試網 http://www.kjueaiud.com/