接口API用例自动转locust测试用例
做接口测试是必要的,写完接口测试用例,再写locust压测脚本,其实差异不大:
写个简单的py,把接口测试脚本转为locust压测脚本,本例只是简单的示范:
原接口校验脚本:
1 # -*- coding = utf-8 -*-
2 # ------------------------------
3 # @time: 2020/8/24 15:59
4 # @Author: drew_gg
5 # @File: exhibition_exhibit_info.py
6 # @Software: api_locust
7 # ------------------------------
8
9 import requests
10
11
12 # need to locust #
13 class KBH:
14
15 host = "https://XXXXer.cn/api"
16 url = "/XXXition/XXibit/info"
17
18 @classmethod
19 def get_exhibition_exhibit_info(cls):
20 """
21 :return:
22 """
23
24 data = {"exhibitId": 34}
25 # 请求参数组装 ## r_url:固定参数
26 r_url = KBH.host + KBH.url
27 # 发起请求
28 r = requests.get(r_url, params=data)
29 if r.json()['data']['info']['id'] and r.status_code == 200:
30 print("success")
31 else:
32 print('error')
33
34
35 if __name__ == "__main__":
36 KBH().get_exhibition_exhibit_info()
1.代码里需要有一下标识:
(1)@File:
(2)import requests
(3)need to locust
(4)class
(5)url
(6)@classmethod
(7)cls
(8)host
(9)r = requests.
大致这些标识
转换后的代码:
# -*- coding = utf-8 -*-
# ------------------------------
# @time: 2020/8/24 15:59
# @Author: drew_gg
# @File: locust_XXXxhibit_info.py
# @Software: api_locust
# ------------------------------ import requests
from locust import task, constant
from locust.contrib.fasthttp import FastHttpUser # need to locust #
class KBH(FastHttpUser): host = "https://XXXXer.cn/api"
url = "/exXXion/XXXibit/info"
wait_time = constant(1) @task
def get_exhibition_exhibit_info(self):
"""
:return:
""" data = {"exhibitId": 34}
# 请求参数组装 ## r_url:固定参数
r_url = KBH.url
# 发起请求
with self.client.get(r_url, params=data, timeout=1, catch_response=True) as r:
if r.content == b"":
r.failure("No data")
if r.status_code != 200:
r.failure("request error")
print(r.json())
如果有多个压测方法的话,按照这个类似循环修改吧,这里只是做个简单的字符匹配与替换
1 # -*- coding = utf-8 -*-
2 # ------------------------------
3 # @time: 2020/8/21 11:54
4 # @Author: drew_gg
5 # @File: case_to_locust.py
6 # @Software: api_locust
7 # ------------------------------
8
9 import os
10
11 pl = os.getcwd().split('api_locust')
12 path_to_do = pl[0] + "api_locust\\locust_view\\kbh_api\\api\\"
13 path_to_end = pl[0] + "api_locust\\locust_view\\kbh_api\\locust_api\\"
14
15
16 def search(path, name):
17 """
18 遍历文档目录
19 :param path:
20 :param name:
21 :return:
22 """
23 file_l = []
24 for root, dirs, files in os.walk(path):
25 root = str(root)
26 if files:
27 for i in files:
28 if name in i:
29 if '__init__' not in i:
30 file_l.append(root + i)
31 return file_l
32
33
34 fl = search(path_to_do, '.py')
35
36 for fi in fl:
37 with open(fi, 'r', encoding="utf-8") as f:
38 py_file = path_to_end + 'locust_' + fi.split('\\')[-1]
39 f_new = open(py_file, 'w', encoding='utf-8')
40 f = f.readlines()
41 class_host = '&&&&&&&&@@@'
42 for i in f:
43 if "need to locust" in i:
44 for line in f:
45 if "@File:" in line:
46 b = "# @File: " + 'locust_' + fi.split('\\')[-1] + '\n'
47 line = line.replace(line, b)
48 if "import" in line:
49 b = line + "from locust import task, constant \nfrom locust.contrib.fasthttp import FastHttpUser\n"
50 line = line.replace(line, b)
51 if "class " in line:
52 b = line.split(":")[0] + "(FastHttpUser):\n"
53 class_name = line.split('class ')[1].split(":")[0]
54 class_host = class_name + ".host + "
55 line = line.replace(line, b)
56 if 'url = "' in line:
57 b = line + " wait_time = constant(1)\n"
58 line = line.replace(line, b)
59 if "@classmethod" in line:
60 line = line.replace(line, " @task\n")
61 if "cls" in line:
62 b = line.split("cls")[0] + 'self' + line.split("cls")[1]
63 line = line.replace(line, b)
64 if class_host in line:
65 b = line.split("KBH.host + ")[0] + line.split("KBH.host + ")[1]
66 line = line.replace(line, b)
67 if "r = requests." in line:
68 r_d = line.split('(')[1].split(")")[0]
69 r_m = line.split('.')[1].split('(')[0]
70 if r_m == "get":
71 b = " with self.client.get(%s, timeout=1, catch_response=True) as r:\n" % r_d
72 if r_m == "post":
73 b = " with self.client.post(%s, timeout=1, catch_response=True) as r:\n" % r_d
74 line = line.replace(line, b)
75 f_new.write(line)
76 b = """ if r.content == b"":
77 r.failure("No data")
78 if r.status_code != 200:
79 r.failure("request error")
80 print(r.json())
81 """
82 f_new.write(b)
83 break
84 f_new.write(line)
85 f_new.close()
应该有其他更好的方式,欢迎交流
Locust QQ 群:

接口API用例自动转locust测试用例的更多相关文章
- 接口如何使用(以笑话大全api为例)
接口如何使用(以笑话大全api为例) 一.总结 一句话总结:直接用ajax,或者post,get方式向接口网址请求数据,然后接收网站传过来的数据就好,和我们写网站的时候前台向后台请求数据的方式一样. ...
- Kafka Consumer API样例
Kafka Consumer API样例 1. 自动确认Offset 说明参照:http://blog.csdn.net/xianzhen376/article/details/51167333 Pr ...
- 微信公众平台接口API
<?php /** * Author: helen * CreateTime: 2015/12/9 20:14 * description: 微信公众平台接口API */ class Wecha ...
- 消息队列接口API(posix 接口和 system v接口)
消息队列 posix API 消息队列(也叫做报文队列)能够克服早期unix通信机制的一些缺点.信号这种通信方式更像\"即时\"的通信方式,它要求接受信号的进程在某个时间范围内对信 ...
- 从壹开始 [ Nuxt.js ] 之二 || 项目搭建 与 接口API
前言 哈喽大家周一好,今天的内容比较多,主要就是包括:把前端页面的展示页给搭出来,然后调通接口API,可以添加数据,这两天我也一直在开发,本来想一篇一篇的写,发现可能会比较简单,就索性把项目搭建的过程 ...
- 推荐一款接口 API 设计神器!
今天栈长给大家推荐一款接口 API 设计神器,传说中的,牛逼哄洪的 Swagger,它到底是什么?今天为大家揭开谜底! Swagger是什么? 官网:https://swagger.io/ Swagg ...
- 接口API封装中常见的HTTP状态码
在进行后端接口API封装的过程中,需要考虑各种错误信息的输出.一般情况下,根据相应问题输出适合的HTTP状态码,可以方便前端快速定位错误,减少沟通成本. HTTP状态码有很多,每个都有对应的含义,下面 ...
- python3.8.0 Django 开发后端接口api 部署到 Linux Centos7上
经历了两天的时候终于把本地使用python3 django开发的接口API部署到服务器上了,还是记录一下,以免之后忘记,哈哈 注意一点,就是,centos7是基于python2的,我这边默认的是pyt ...
- ng接口API开发文档
接口版本:v1 接口协议:请商户对接使用对应的转账接口API和免转接口API,商户只能使用菜单对应的API,否则接口会调用失败.左侧菜单未注明的接口免转钱包和转账钱包可以共同使用所有采集均按照北京时间 ...
- Vue学习笔记-Django REST framework3后端接口API学习
一 使用环境 开发系统: windows 后端IDE: PyCharm 前端IDE: VSCode 数据库: msyql,navicat 编程语言: python3.7 (Windows x86- ...
随机推荐
- vue开发过程常用的JSX语法
参考资料:https://juejin.cn/post/7114063575122984973 在Vue项目的开发过程,经常会使用到JSX语法,对常用的JSX语法分类做个笔记,方便需要之时查阅 动态绑 ...
- liunx 设置默认python版本方法,
Linux 中把Python3设为默认Python版本的几种方法 由于工作中要用到到python3.6 而服务器是2.7 ,这个低版本的2.7很多系统都要依赖,还不能删,同事建议建一个虚拟环境,但是 ...
- Spring事务(六)-只读事务
@Transactional(readOnly=true)就可以把事务方法设置成只读事务.设置了只读事务,事务从开始到结束,将看不见其他事务所提交的数据.这在某种程度上解决了事务并发的问题.一个方法内 ...
- Zabbix“专家坐诊”第186期问答汇总
问题一 Q:这两个键值vm.memory.size[pused]和vm.memory.util监控内存使用率有什么区别,使用那个监控使用率更好,支持windows系统和Linux系统么,对agent端 ...
- 深入解析:AntSK 0.1.7版本的技术革新与多模型管理策略
在信息技术快速迭代的当下,.Net生态中的AntSK项目凭借其前沿的AI知识库和智能体技术,已经吸引了广大开发者的关注和参与.今天,我要给大家介绍的主角,AntSK 0.1.7版本,无疑将是这个开源项 ...
- [VueJsDev] 日志 - nginxConfig 配置文件备份
[VueJsDev] 目录列表 https://www.cnblogs.com/pengchenggang/p/17037320.html nginxConfig 配置文件备份 ::: details ...
- Template String Converter - 字符串中加变量 自动将单引号变换 - vscode插件
Template String Converter - 字符串中加变量 自动将单引号变换 - vscode插件
- 13_AAC编码介绍
AAC(Advanced Audio Coding,译为:高级音频编码),是由Fraunhofer IIS.杜比实验室.AT&T.Sony.Nokia等公司共同开发的有损音频编码和文件格式. ...
- js使用typeof与instanceof相结合编写一个判断常见变量类型的函数
/** * 常见类型判断 * @param {any} param */ function getParamType(param) { // 先判断是否能用typeof 直接判断 let types1 ...
- YUM以及yum源搭建
1 YUM简介 1.1 YUM简介 CentOS使用yum和dnf 解决rpm的包依赖关系. YUM:rpm的前端程序,可解决软件包相关依赖性,可在多个库之间定位软件包,up2date的替代工具,Ce ...