接口测试需求:

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. C# 私有字段前缀 _ 的设置(VS2019, .editorconfig)

    常量和静态只读字段大写 私有字段前缀 _ #### Naming styles #### # Naming rules dotnet_naming_rule.const_should_be_all_u ...

  2. JSON对象及方法

    1.JSON JSON 包括 JSON 字符串和 JSON 对象.JSON 通常用于与服务端交换数据,在给服务器接收和发送数据时用的都是字符串,可以是 JSON 字符串或者一般的键值对字符串.把Jav ...

  3. codeforces 111A/112C Petya and Inequiations

    题目:Petya and Inequiations传送门: http://codeforces.com/problemset/problem/111/A http://codeforces.com/p ...

  4. 神秘的java Https

    说起网络安全,最基本的策略就是走https.https仿佛一条神秘通道,有了它,万事无忧. 究竟什么是https?如何实现https? 本文将揭开https的神秘面纱. WTF https 万事皆有源 ...

  5. <HTTP权威指南>记录 ---- 网络爬虫

    网络爬虫 网络爬虫(web crawler)能够在无需人类干预的情况下自动进行一系列Web事务处理的软件程序.很多爬虫会从一个Web站点逛到另一个Web站点,获取内容,跟踪超链,并对它们找到的数据进行 ...

  6. 2018-2019-2 20175223 实验四 《Android开发基础》实验报告

    目录 北京电子科技学院(BESTI)实验报告 实验名称:实验四 Android程序设计 实验内容.步骤与体会: 一.实验四 Android程序设计-1 二.实验四 Android程序设计-2 三.实验 ...

  7. 解决myeclipse validation验证javascript导致速度变慢的现象

    参考:https://jingyan.baidu.com/article/ca41422fe094251eae99ede7.html

  8. 正则findall的使用

    import re title = 'hello, 你好,world' print(title) title = u'hello, 你好,world' print(title) #汉字匹配 +的意思是 ...

  9. Cocos2d-x之UI控件简介

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 在Cocos2d-x中提供了一种UI控件,UI控件是指用户交互界面的操作部件.UI控件可以是Button(按钮),CheckBox(选择框 ...

  10. css怎么解决表格边框线重复问题

    <ul> <li><a href="#">1</a></li> <li><a href="# ...