接口测试需求:

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. Python3解leetcode Binary Tree PathsAdd Digits

    问题描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

  2. soj#552 449E Jzzhu and Squares

    分析 https://www.cnblogs.com/Memory-of-winter/p/11209128.html 代码 #include<bits/stdc++.h> using n ...

  3. CreateProcessEx创建进程

    NTSYSCALLAPI NTSTATUS NTAPI NtCreateProcess( OUT PHANDLE ProcessHandle, IN ACCESS_MASK DesiredAccess ...

  4. PAT_A1069#The Black Hole of Numbers

    Source: PAT A1069 The Black Hole of Numbers (20 分) Description: For any 4-digit integer except the o ...

  5. 带有headers的urllib库爬取

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

  6. HTML创建文本框的3种方式

    我的第一个随笔,记录主要用来整理学习的知识点 1.input 创建单行文本框 <input type="text" size="10" maxlength ...

  7. Debian 防火墙 打开 关闭

    Debian原来用的是UFW防火墙,之前没接触过这种类型防火墙,懵逼了半天,这里记录一下简单的使用规则,后期在使用过程中慢慢完善UFW防火墙的使用操作方法: 查看防火墙现有规则: ufw status ...

  8. [转]图片压缩功能在JBoss AS7出错有关问题

    图片压缩功能在JBoss AS7出错问题近日在为平台(http://www.zyiqibook.com)做些小的整改,途中基本很顺利,但就在后面出现了些纠结的问题,而这问题我之前遇到过,因为一时没有想 ...

  9. Vue-基础(二)

    一,Vue中的组件化开发: Vue中的组件也就是Vue实例 组件类型: 通用组件(例如,表单,弹窗,布局类等) 业务组件(抽奖,机器分类) 页面组件(单页面开发程序的每个页面都是一个组件) 组件开发三 ...

  10. 获取年月日格式为yyyy-m-d简单写法

    方法:通过日期函数toLocaleString()获取.new Date().toLocaleString( );  //"2019/9/1 上午11:20:23" 获取年月日方法 ...