接口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- ...
随机推荐
- AtCoder Beginner Contest 338(A~E补题)
目录 A B C题 D题 E题 A 签到 #include <bits/stdc++.h> #define rep(i,a,b) for(int i = (a); i <= (b); ...
- Git修改最近一次提交的日志信息
一.问题由来 当前自己所在的项目组中,每次发完一个版本后,就需要创建个人新版本的git提交凭证,其实就是系统自动 生成的一串编码,如果没有这个凭证,代码是提交不了的,这是公司制定的开发规范之一.这两天 ...
- iview viewDesign table 单选 Radio
columns.js export default [ { title: '选择', slot: 'choose', align: 'center', width: 100 }, table里面 &l ...
- viewui tree 自定义化(源码copy出来改动)#添加 获取selected 解决方案
需求: 对树有title的点击效果,右侧有说明文字 和 按钮能点击,不能右键,系统用的壳已经有右键了. 出现的问题: viewui的tree有两年没更新了,对ui这块,是采取的render自定义,但是 ...
- Kotlin 快速遍历File及子目录筛选指定类型文件
原文: Kotlin 快速遍历File及子目录筛选指定类型文件 - Stars-One的杂货小窝 在做文件相关的app,经常会遇到筛选某个文件夹下的符合条件的文件对象,且要包含子文件夹,之前一直是自己 ...
- vscode自动生成头文件
Ctrl Shift P 输入:snipp,选配置用户代码片段,新建全局代码片段文件,修改下列模板: { // Place your 全局 snippets here. Each snippet is ...
- Python配置文件使用教程
在 Python 应用程序开发过程中,配置文件扮演着重要的角色.配置文件可以用来存储应用程序的各种设置.选项和参数,使得程序更加灵活和可配置.本文将介绍 Python 中如何使用配置文件,并提供一些常 ...
- WPF设置传参、点击按钮改变长度
前台代码 <!-- 通过传递ElementName --> <Button x:Name="btn1" Width="200" Height= ...
- RageFrame学习笔记:创建路由+导入layui
这是我写的学习RageFrame的第二篇,这一篇给大家分享下我是如何创建路由,导入外部js,css文件的,这里写下我的全部流程,希望对大家有所帮助. 话不多说,直接开始,在上一章中,我们已经把项目实例 ...
- Docker网络模型以及容器通信
本篇接着上篇:[Docker0网络及原理探究],继续深入探究容器网络通信原理,通过学习Docker网路驱动模型,更好地解决容器间的通信问题 1.Docker的网络驱动模型 1.1.Docker的网络驱 ...