接口测试需求:

1、脚本支持循环测试,并且每次测试的请求报文不一样(字段stbId每次请求不一样)

2、输出每次测试的请求报文和响应报文

3、根据响应报文判断接口调用是否成功

4、输出最终测试结果:循环次数,成功次数,失败次数

请求报文样例:

{
"appId":"",
"stbId":"stbId_123456789",
"userId":"userId_123456789",
"ppvId":"ppvId_123456789",
"contentId":"contentId_123456789",
"transationId":"transationId_123456789",
"serviceType":"",
"recordTime":""
}

响应报文样例:

{
"result":"",
"resultNotes":"",
"transationId":"transationId_123456789"
}

测试脚本:vuser_init、Action、vuser_end、globals.h

vuser_init():

vuser_init()
{
return ;
}

Action():

Action()
{
// 定义请求报文的URL
char requestUrl[] = "URL=http://21.254.43.61:6663/eps/eps?command=freeTrialNotice"; // 定义请求报文的BODY,对参数stbId参数化
char requestBody[] = "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}"))==){
lr_output_message("result为%s,接口调用成功",lr_eval_string("{result}"));
// 统计成功数
countSuccess += ;
}else{
lr_output_message("result为%s,接口调用失败",lr_eval_string("{result}"));
// 统计失败数
countFailed += ;
} return ;
}

vuser_end():

vuser_end()
{
// 打印最终的测试结果
lr_output_message("循环次数:%d,成功数:%d,失败数:%d",countSuccess + countFailed,countSuccess,countFailed);
return ;
}

globals.h:

#ifndef _GLOBALS_H
#define _GLOBALS_H //--------------------------------------------------------------------
// Include Files
#include "lrun.h"
#include "web_api.h"
#include "lrw_custom_body.h" // 定义全局变量 成功次数
int countSuccess = ; // 定义全局变量 失败次数
int countFailed = ; //--------------------------------------------------------------------
// Global Variables #endif // _GLOBALS_H

脚本循环执行3次,结果如下:

Virtual User Script started at : -- ::
Starting action vuser_init.
Web Turbo Replay of LoadRunner 11.0. for Windows ; build (Aug ::) [MsgId: MMSG-]
Run Mode: HTML [MsgId: MMSG-]
Run-Time Settings file: "E:\VU\freeTrialNotice\\default.cfg" [MsgId: MMSG-]
Ending action vuser_init.
Running Vuser...
Starting iteration .
Starting action Action.
Action.c(): Registering web_reg_save_param was successful [MsgId: MMSG-]
Action.c(): Registering web_reg_save_param was successful [MsgId: MMSG-]
Action.c(): Registering web_reg_save_param was successful [MsgId: MMSG-]
Action.c(): web_custom_request("freeTrialNoticeTest") was successful, body bytes, header bytes [MsgId: MMSG-]
Action.c():
请求报文URL=http://21.254.43.61:6663/eps/eps?command=freeTrialNotice
请求报文BODY=
{
"appId":"",
"stbId":"{freeTrialNotice}",
"userId":"userId_123456789",
"ppvId":"ppvId_123456789",
"contentId":"contentId_123456789",
"transationId":"transationId_123456789",
"serviceType":"",
"recordTime":""
}
Action.c(): freeTrialNotice=stbId_123456789
Action.c(): 响应报文:
{
"result":"",
"resultNotes":"",
"transationId":"transationId_123456789"
}
Action.c(): result为0,接口调用成功
Ending action Action.
Ending iteration .
Starting iteration .
Starting action Action.
Action.c(): Registering web_reg_save_param was successful [MsgId: MMSG-]
Action.c(): Registering web_reg_save_param was successful [MsgId: MMSG-]
Action.c(): Registering web_reg_save_param was successful [MsgId: MMSG-]
Action.c(): web_custom_request("freeTrialNoticeTest") was successful, body bytes, header bytes [MsgId: MMSG-]
Action.c():
请求报文URL=http://21.254.43.61:6663/eps/eps?command=freeTrialNotice
请求报文BODY=
{
"appId":"",
"stbId":"{freeTrialNotice}",
"userId":"userId_123456789",
"ppvId":"ppvId_123456789",
"contentId":"contentId_123456789",
"transationId":"transationId_123456789",
"serviceType":"",
"recordTime":""
}
Action.c(): freeTrialNotice=stbId_12345678
Action.c(): 响应报文:
{
"result":"",
"resultNotes":"",
"transationId":"transationId_123456789"
}
Action.c(): result为0,接口调用成功
Ending action Action.
Ending iteration .
Starting iteration .
Starting action Action.
Action.c(): Registering web_reg_save_param was successful [MsgId: MMSG-]
Action.c(): Registering web_reg_save_param was successful [MsgId: MMSG-]
Action.c(): Registering web_reg_save_param was successful [MsgId: MMSG-]
Action.c(): web_custom_request("freeTrialNoticeTest") was successful, body bytes, header bytes [MsgId: MMSG-]
Action.c():
请求报文URL=http://21.254.43.61:6663/eps/eps?command=freeTrialNotice
请求报文BODY=
{
"appId":"",
"stbId":"{freeTrialNotice}",
"userId":"userId_123456789",
"ppvId":"ppvId_123456789",
"contentId":"contentId_123456789",
"transationId":"transationId_123456789",
"serviceType":"",
"recordTime":""
}
Action.c(): freeTrialNotice=
Action.c(): 响应报文:
{
"result":"",
"resultNotes":"蹇呭~鍙傛暟瀛樺湪绌哄€硷紒",
"transationId":"transationId_123456789"
}
Action.c(): result为1,接口调用失败
Ending action Action.
Ending iteration .
Ending Vuser...
Starting action vuser_end.
vuser_end.c(): 循环次数:,成功数:,失败数:
Ending action vuser_end.
Vuser Terminated.

loadRunner之接口测试的更多相关文章

  1. 基于LoadRunner构建接口测试框架

    基于LoadRunner构建接口测试框架 http://www.docin.com/p-775544153.html

  2. 用LoadRunner实现接口测试

    接口测试的两种方法 其实无论用那种测试方法,接口测试的原理是通过测试程序模拟客户端向服务器发送请求报文,服务器接收请求报文后对相应的报文做出处理然后再把应答报文发送给客户端,客户端接收应答报文这一个过 ...

  3. 使用Loadrunner进行接口测试

    在工作中很多时候都需要进行接口测试,如果只是进行接口的功能测试这个很简单,两种类型: 1.如果是基于get的直接使用浏览器进行访问,查看服务器返回的数据是否正确就行, 2.如果是基于post的可以接触 ...

  4. Jmeter、Postman 、 loadrunner SoapUI 接口测试工具

    一. loadrunner  简称 LR 二. Jmeter 1.安装包:apache-jmeter-4.0.tgz   解压.学会此工具的使用  和POSTman 一样的. 2.本机测试:双击apa ...

  5. LoadRunner Vuser接口测试脚本 Post举例

    发送内容为JSON的POST请求需要加入"EncType=application/json",表单参数不需要.(JAVA API) // POST接口调用 web.custom_r ...

  6. Loadrunner 接口测试的两种方法

    其实无论用那种测试方法,接口测试的原理是通过测试程序模拟客户端向服务器发送请求报文,服务器接收请求报文后对相应的报文做出处理然后再把应答报文发送给客户端,客户端接收应答报文这一个过程. 方法一.用Lo ...

  7. Jenkins+ANT+Jmeter 接口测试的实践(转载)

    转载地址:https://testerhome.com/topics/5262 1.前言 最近感觉大家都在讲Jenkins+jmeter+ant或maven的使用,但没有说到具体怎么投入到项目使用,只 ...

  8. LR进行接口测试

    其实无论用那种测试方法,接口测试的原理是通过测试程序模拟客户端向服务器发送请求报文,服务器接收请求报文后对相应的报文做出处理然后再把应答报文发送给客户端,客户端接收应答报文这一个过程. 方法一.用Lo ...

  9. web_custom_request函数做get接口测试

    最近研究了使用loadrunner做接口测试,刚开始一直不成功,后来加了QQ群,遇到大神了,经指导终于成功 下面是具体实例代码: //{"signIOS":1,"sign ...

随机推荐

  1. Oracle 包的学习

    (1)包是一种数据库对象,相当于一个容器.将逻辑上相关的过程.函数.变量.常量和游标组合成一个更大的单位.用户可以从其他 PL/SQL 块中对其进行引用 (2)包类似于C++和JAVA语言中的类,其中 ...

  2. Delphi界面篇之ListView控件

    //增加项或列(字段) ListView1.Clear; ListView1.Columns.Clear; ListView1.Columns.Add; ListView1.Columns.Add; ...

  3. TestComplete 14 百度网盘下载

    TestComplete 14 百度网盘下载 链接:https://pan.baidu.com/s/1g3imm9zFOCKnx5qqtUMl4g 二维码:

  4. Gson字符串转换对象数组

    public class Input { private String title; private int formId; private String content; public String ...

  5. MultipartFile 图片上传到Linux服务器Tomcat下的webapps目录

    第一次接触 linux 服务器,做图片上传的时候遇到了些坑,搞了些天总算成功了,记录一下 /** * 上传图片 * * @param request * @param file * @return * ...

  6. 关于tomcat中的三个端口的作用及其相关细节

    [一]端口内容 tomcat的端口号相关信息: Tomcat admin port——管理端口,允许你远程配置tomcat HTTP——正常的http协议 AJP——Apache JServ Prot ...

  7. 带有headers的urllib库爬取

    #请求头 #1.引入模块 from urllib import request #2.操作 #(1)定义目标url base_url = "http://www.langlang2017.c ...

  8. PAT甲级【2019年3月考题】——A1156 SexyPrimes【20】

    Sexy primes are pairs of primes of the form (p, p+6), so-named since “sex” is the Latin word for “si ...

  9. CentOS中svn的搭建

    1:使用yum源进行安装 # rpm -qa subversion //检查是否自带了低版本的svn #yum remove subversion //卸载低版本的svn #Yum install s ...

  10. subprocess 模块 与 re 模块

    sub :子 process:进程 用法: import subprocess while True: cmd_str = inport('请输入终端命令:') obj = subprocrss.Po ...