接口测试需求:
1、脚本支持循环测试,并且每次测试的请求报文不一样(字段stbId每次请求不一样)
2、输出每次测试的请求报文和响应报文
3、根据响应报文判断接口调用是否成功
4、输出最终测试结果:循环次数,成功次数,失败次数
请求报文样例:
{ "appId":"10101", "stbId":"stbId_123456789", "userId":"userId_123456789", "ppvId":"ppvId_123456789", "contentId":"contentId_123456789", "transationId":"transationId_123456789", "serviceType":"2", "recordTime":"20150115105332" }
响应报文样例:
{ "result":"0", "resultNotes":"", "transationId":"transationId_123456789" }
测试脚本:vuser_init、Action、vuser_end、globals.h
vuser_init():
vuser_init() { return 0; }
Action():
Action() { // 定义请求报文的URL char requestUrl[1024] = "URL=http://21.254.43.61:6663/eps/eps?command=freeTrialNotice"; // 定义请求报文的BODY,对参数stbId参数化 char requestBody[1024] = "BODY=" "\n{\n" " \"appId\":\"10101\",\n" " \"stbId\":\"{freeTrialNotice}\",\n" " \"userId\":\"userId_123456789\",\n" " \"ppvId\":\"ppvId_123456789\",\n" " \"contentId\":\"contentId_123456789\",\n" " \"transationId\":\"transationId_123456789\",\n" " \"serviceType\":\"2\",\n" " \"recordTime\":\"20150115105332\"\n" "}\n"; // 从响应报文中获取参数result web_reg_save_param("result", "LB=\"result\":\"", "RB=\",", LAST); // 从响应报文中获取参数resultNotes web_reg_save_param("resultNotes", "LB=\"resultNotes\":\"", "RB=\",", LAST); // 从响应报文中获取参数transationId web_reg_save_param("transationId", "LB=\"transationId\":\"", "RB=\"", LAST); // 发起请求 web_custom_request("freeTrialNoticeTest", "Method=POST", "RecContentType=text/html", requestUrl, requestBody, LAST); // 打印请求报文 lr_output_message("\n请求报文%s\n请求报文%s",requestUrl,requestBody); // 打印freeTrialNotice变量值 lr_output_message("freeTrialNotice=%s",lr_eval_string("{freeTrialNotice}")); // 打印响应报文 lr_output_message("响应报文:\n" "{\n" " \"result\":\"%s\",\n" " \"resultNotes\":\"%s\",\n" " \"transationId\":\"%s\"\n" "}\n", lr_eval_string("{result}"), lr_eval_string("{resultNotes}"), lr_eval_string("{transationId}")); // 根据返回的result判断接口调用是否成功,0:成功,非0:失败 if(atoi(lr_eval_string("{result}"))==0){ lr_output_message("result为%s,接口调用成功",lr_eval_string("{result}")); // 统计成功数 countSuccess += 1; }else{ lr_output_message("result为%s,接口调用失败",lr_eval_string("{result}")); // 统计失败数 countFailed += 1; } return 0; }
vuser_end():
vuser_end() { // 打印最终的测试结果 lr_output_message("循环次数:%d,成功数:%d,失败数:%d",countSuccess + countFailed,countSuccess,countFailed); return 0; }
globals.h:
#ifndef _GLOBALS_H #define _GLOBALS_H //-------------------------------------------------------------------- // Include Files #include "lrun.h" #include "web_api.h" #include "lrw_custom_body.h" // 定义全局变量 成功次数 int countSuccess = 0; // 定义全局变量 失败次数 int countFailed = 0; //-------------------------------------------------------------------- // Global Variables #endif // _GLOBALS_H
脚本循环执行3次,结果如下:
Virtual User Script started at : 2015-05-15 12:08:38 Starting action vuser_init. Web Turbo Replay of LoadRunner 11.0.0 for Windows 7; build 8859 (Aug 18 2010 20:14:31) [MsgId: MMSG-27143] Run Mode: HTML [MsgId: MMSG-26000] Run-Time Settings file: "E:\VU\freeTrialNotice\\default.cfg" [MsgId: MMSG-27141] Ending action vuser_init. Running Vuser... Starting iteration 1. Starting action Action. Action.c(20): Registering web_reg_save_param was successful [MsgId: MMSG-26390] Action.c(26): Registering web_reg_save_param was successful [MsgId: MMSG-26390] Action.c(32): Registering web_reg_save_param was successful [MsgId: MMSG-26390] Action.c(38): web_custom_request("freeTrialNoticeTest") was successful, 78 body bytes, 142 header bytes [MsgId: MMSG-26386] Action.c(46): 请求报文URL=http://21.254.43.61:6663/eps/eps?command=freeTrialNotice 请求报文BODY= { "appId":"10101", "stbId":"{freeTrialNotice}", "userId":"userId_123456789", "ppvId":"ppvId_123456789", "contentId":"contentId_123456789", "transationId":"transationId_123456789", "serviceType":"2", "recordTime":"20150115105332" } Action.c(49): freeTrialNotice=stbId_123456789 Action.c(52): 响应报文: { "result":"0", "resultNotes":"", "transationId":"transationId_123456789" } Action.c(64): result为0,接口调用成功 Ending action Action. Ending iteration 1. Starting iteration 2. Starting action Action. Action.c(20): Registering web_reg_save_param was successful [MsgId: MMSG-26390] Action.c(26): Registering web_reg_save_param was successful [MsgId: MMSG-26390] Action.c(32): Registering web_reg_save_param was successful [MsgId: MMSG-26390] Action.c(38): web_custom_request("freeTrialNoticeTest") was successful, 78 body bytes, 142 header bytes [MsgId: MMSG-26386] Action.c(46): 请求报文URL=http://21.254.43.61:6663/eps/eps?command=freeTrialNotice 请求报文BODY= { "appId":"10101", "stbId":"{freeTrialNotice}", "userId":"userId_123456789", "ppvId":"ppvId_123456789", "contentId":"contentId_123456789", "transationId":"transationId_123456789", "serviceType":"2", "recordTime":"20150115105332" } Action.c(49): freeTrialNotice=stbId_12345678 Action.c(52): 响应报文: { "result":"0", "resultNotes":"", "transationId":"transationId_123456789" } Action.c(64): result为0,接口调用成功 Ending action Action. Ending iteration 2. Starting iteration 3. Starting action Action. Action.c(20): Registering web_reg_save_param was successful [MsgId: MMSG-26390] Action.c(26): Registering web_reg_save_param was successful [MsgId: MMSG-26390] Action.c(32): Registering web_reg_save_param was successful [MsgId: MMSG-26390] Action.c(38): web_custom_request("freeTrialNoticeTest") was successful, 105 body bytes, 143 header bytes [MsgId: MMSG-26386] Action.c(46): 请求报文URL=http://21.254.43.61:6663/eps/eps?command=freeTrialNotice 请求报文BODY= { "appId":"10101", "stbId":"{freeTrialNotice}", "userId":"userId_123456789", "ppvId":"ppvId_123456789", "contentId":"contentId_123456789", "transationId":"transationId_123456789", "serviceType":"2", "recordTime":"20150115105332" } Action.c(49): freeTrialNotice= Action.c(52): 响应报文: { "result":"1", "resultNotes":"蹇呭~鍙傛暟瀛樺湪绌哄€硷紒", "transationId":"transationId_123456789" } Action.c(68): result为1,接口调用失败 Ending action Action. Ending iteration 3. Ending Vuser... Starting action vuser_end. vuser_end.c(4): 循环次数:3,成功数:2,失败数:1 Ending action vuser_end. Vuser Terminated.
时间: 2024-11-05 18:49:48