最近公司的項目打算實現(xiàn)平臺化,各個模塊之間都是以接口形式提供,但是公司又沒那么多測試人員,并且頻繁上線,導致產(chǎn)品質(zhì)量把關(guān)不嚴,所以我想能不能對這些接口進行自動化測試呢,由于做性能測試時對loadrunner使用較熟練,所以產(chǎn)生了使用loadrunner對這些接口進行自動化測試的想法。
我的想法是測試數(shù)據(jù)也就是測試用例數(shù)據(jù)庫可以用參數(shù)化來實現(xiàn),測試結(jié)果的檢測既可以用檢查點來實現(xiàn)也可以使用關(guān)聯(lián)抓取響應的值,通過與預期結(jié)果進行比較來實現(xiàn),具體的做法可以參考網(wǎng)上一篇文章:
需要取得的輸入應預先制作了CSV文件,關(guān)在腳本參數(shù)配置中定義變量。
自動化測試程序關(guān)鍵代碼
1、生成結(jié)果文件(html格式),文件名稱為 test _系統(tǒng)時間(%Y%m%d%H%M%S)_虛擬用戶編號,并寫入測試結(jié)果文件的html開始標識
CODE:
//定義結(jié)果文件變量
long file;
//定義文件名種子(虛擬用戶編號)變量
char *vusernum;
//定義測試結(jié)果變量
char V_Result[1024];
vuser_init()
{
//取得文件名種子(虛擬用戶編號)
vusernum=lr_eval_string ("_{vuserid}");
//取得文件種子(系統(tǒng)時間)
lr_save_datetime("%Y%m%d%H%M%S", DATE_NOW, "now_date");
//拼結(jié)測試結(jié)果文件名稱
strcpy(V_Result,"d://test/Result/test");
strcat(V_Result,lr_eval_string("_{now_date}"));
strcat(V_Result,vusernum);
strcat(V_Result,".html");
//生成并打開測試結(jié)果文件
file=fopen(V_Result,"at+");
//寫入測試文件頭部html信息
strcpy(V_Result,"<html><table border='1'><tr>< td>IMSI號碼</td><td>預期值</td><td>返回值< /td><td>結(jié)果</td></tr>");
fputs(V_Result,file);
return 0;
}2、從參數(shù)化文件讀取測試參數(shù)和預期結(jié)果、發(fā)送請求并獲得服務器返回實際結(jié)果,比較測試結(jié)果后寫入測試結(jié)果文件。
CODE:
Action()
{
//測試結(jié)果文本
char V_testres[1024];
//定義返回結(jié)果是否正確變量
int result;
//取得IMSI號碼
char *V_imsi=lr_eval_string ("{IMSI}");
//設置頁面接收最大的字節(jié)數(shù),該設置應大于服務器返回內(nèi)容的大小
web_set_max_html_param_len("20000");
//取得服務器返回內(nèi)容
web_reg_save_param("filecontent",
"LB=",
"RB=",
"Search=Body",
LAST);
//發(fā)送請求
web_submit_data("login",
"Action=http://host:port/autonavit/search?cmd=clientlogin&termver=5&termcode=30001&termdbver=3 ",
"Method=POST",
"RecContentType=text/html",
"Referer=",
"Snapshot=t9.inf",
"Mode=HTTP",
ITEMDATA,
"Name=imsi", "Value={IMSI}", ENDITEM,
LAST);
//比較預期值和實際值是否相等
result=strcmp(lr_eval_string("{YQJG}"),lr_eval_string("{filecontent}"));
if ( result == 0 )
{
strcpy(V_testres,"通過");
}
else
{
strcpy(V_testres,"失敗");
}
strcpy(V_Result,"<tr><td>");
//寫入測試參數(shù)
strcat(V_Result,V_imsi);
strcat(V_Result,"</td>");
strcat(V_Result,"<td id='yq'>");
//寫入預期結(jié)果
strcat(V_Result,lr_eval_string("{YQJG}"));
strcat(V_Result,"</td>");
strcat(V_Result,"<td id='sj'>");
//寫入實際結(jié)果
strcat(V_Result,lr_eval_string("{filecontent}"));
strcat(V_Result,"</td>");
strcat(V_Result,"<td>");
//寫入測試是否通過
strcat(V_Result, V_testres);
strcat(V_Result,"</td></tr>");
fputs(V_Result,file);
return 0;
}3、寫入測試結(jié)果文件尾部html信息,關(guān)閉文件并結(jié)束測試。
CODE:
vuser_end()
{
//結(jié)束并關(guān)閉文件
strcpy(V_Result,"</table></html>");
fputs(V_Result,file);
fclose(file);
return 0;
}