接口测试需求:

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. boost multi array

    Boost MultiArray is a library that simplifies using arrays with multiple dimensions. 1. #include < ...

  2. Linux进程管理工具vmstat,iostat,pmap

    一查看内存的工具——vmstat (一)vmstat的介绍 vmstat vmstat是Virtual Memory Statistics(虚拟内存统计)的缩写 利用vmstat命令可以对操作系统的报 ...

  3. kafka-producer.properties

    # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreement ...

  4. win7搭建局域网时间服务器

    近日,本人想在局域网内通过普通的windows 7 PC搭建一台NTP服务器,可看似简单的配置却给我捣腾了了半天.初期,参考了互联网的上相关的配置文档,可网络设备就是死活不同步NTP服务器的时间.实在 ...

  5. 边缘节点 如何判断CDN的预热任务是否执行完成刷新 路由追踪 近期最少使用算法

    阿里云内容分发网络(Content Delivery Network,简称CDN)是建立并覆盖在承载网之上,由分布在不同区域的边缘节点服务器群组成的分布式网络.阿里云CDN分担源站压力,避免网络拥塞, ...

  6. The SDK directory '/home/wangju/gitProject/Automation/D:\Android_SDK' does not exist.

    执行gradle clean命令报错 gradle clean FAILURE: Build failed with an exception. * What went wrong: A proble ...

  7. nginx配置相关问题

    1. nginx配置ssl相关问题 1.1 报错nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in ...

  8. centos 7 安装 redis 及 php-redis 拓展

    ===============redis 安装========================== 直接yum 安装的redis 不是最新版本 yum install redis 如果要安装最新的re ...

  9. PAT 2019-3 7-2 Anniversary

    Description: Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for ...

  10. 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 ...