|
最近公司的項(xiàng)目打算實(shí)現(xiàn)平臺(tái)化,各個(gè)模塊之間都是以接口形式提供,但是公司又沒那么多測試人員,并且頻繁上線,導(dǎo)致產(chǎn)品質(zhì)量把關(guān)不嚴(yán),所以我想能不能對(duì)這些接口進(jìn)行自動(dòng)化測試呢,由于做性能測試時(shí)對(duì)loadrunner使用較熟練,所以產(chǎn)生了使用loadrunner對(duì)這些接口進(jìn)行自動(dòng)化測試的想法。
我的想法是測試數(shù)據(jù)也就是測試用例數(shù)據(jù)庫可以用參數(shù)化來實(shí)現(xiàn),測試結(jié)果的檢測既可以用檢查點(diǎn)來實(shí)現(xiàn)也可以使用關(guān)聯(lián)抓取響應(yīng)的值,通過與預(yù)期結(jié)果進(jìn)行比較來實(shí)現(xiàn),具體的做法可以參考網(wǎng)上一篇文章:
需要取得的輸入應(yīng)預(yù)先制作了CSV文件,關(guān)在腳本參數(shù)配置中定義變量。
自動(dòng)化測試程序關(guān)鍵代碼
1、生成結(jié)果文件(html格式),文件名稱為 test _系統(tǒng)時(shí)間(%Y%m%d%H%M%S)_虛擬用戶編號(hào),并寫入測試結(jié)果文件的html開始標(biāo)識(shí)
CODE:
//定義結(jié)果文件變量
long file;
//定義文件名種子(虛擬用戶編號(hào))變量
char *vusernum;
//定義測試結(jié)果變量
char V_Result[1024];
vuser_init()
{
//取得文件名種子(虛擬用戶編號(hào))
vusernum=lr_eval_string ("_{vuserid}");
//取得文件種子(系統(tǒng)時(shí)間)
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號(hào)碼</td><td>預(yù)期值</td><td>返回值< /td><td>結(jié)果</td></tr>");
fputs(V_Result,file);
return 0;
}2、從參數(shù)化文件讀取測試參數(shù)和預(yù)期結(jié)果、發(fā)送請(qǐng)求并獲得服務(wù)器返回實(shí)際結(jié)果,比較測試結(jié)果后寫入測試結(jié)果文件。
CODE:
Action()
{
//測試結(jié)果文本
char V_testres[1024];
//定義返回結(jié)果是否正確變量
int result;
//取得IMSI號(hào)碼
char *V_imsi=lr_eval_string ("{IMSI}");
//設(shè)置頁面接收最大的字節(jié)數(shù),該設(shè)置應(yīng)大于服務(wù)器返回內(nèi)容的大小
web_set_max_html_param_len("20000");
//取得服務(wù)器返回內(nèi)容
web_reg_save_param("filecontent",
"LB=",
"RB=",
"Search=Body",
LAST);
//發(fā)送請(qǐng)求
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);
//比較預(yù)期值和實(shí)際值是否相等
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'>");
//寫入預(yù)期結(jié)果
strcat(V_Result,lr_eval_string("{YQJG}"));
strcat(V_Result,"</td>");
strcat(V_Result,"<td id='sj'>");
//寫入實(shí)際結(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;
}
|