吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2077|回复: 2
收起左侧

[Python 转载] [python] 生成接口测试用例 半成品

  [复制链接]
华夏利剑 发表于 2021-2-9 14:59
[Python] 纯文本查看 复制代码
#  -* - encoding: utf -8  -* -
import json
import copy
import sys

#  readme  介绍和问题如下
"""
我想根据a,b生成如下数据

生成一个新的json格式文件
json格式同postman.json格式一样

主要难点在 fromdata处理上

formdata是一个list,list中是字典
有三个元素 key values type
key  = a.keys()
value for  b
b.append(a.values) 把a中的value加入到b中

生成的数据需要根据 a和b的长度来定

脚本现在生成的value 是无法改变的,希望大佬可以指导一下我

"""
# - - - - - - - - - - - - -自定义接口信息 - - - - - - - - - - - - - - - -#

interFaceName = "接口测试用例"
bodyMode = "formdata"
requestMethod = "POST"
requestHost = "www.baidu.com"
requestUrl = "https://www.baidu.com"
# - - - - - - - - - - - - -自定义接口信息 - - - - - - - - - - - - - - - -#

# formdata临时变量
tmp = []
data = {}
info = {}
itemL = []
itemD = {}

protocolProfileBehavior = {}
disabledSystemHeadersD = {}

requestD = {}
headerL = []
headerD = {}
bodyD = {}
formdataL = []
urlD = {}
responseL = ["json_response"]

#  - - - - - - - - - - -info - - - - - - - - - - - - - -
info["_postman_id"] = "0a88f277"
info["name"] = interFaceName
info["schema"] = "schema.getpostman.com"

#  - - - - - - - - - - -header - - - - - - - - - - - - - -
headerD["key"] = "Content -Type"
headerD["value"] = "application/json; charset=UTF -8"
headerD["type"] = "text"
headerL.append(headerD)

#  - - - - - - - - - - -request - - - - - - - - - - - - - -
requestD["method"] = requestMethod
requestD["header"] = headerL
requestD["body"] = bodyD
requestD["url"] = urlD
requestD["reponse"] = responseL

#  - - - - - - - - - - -入参 - - - - - - - - - - - - - -
a = {"A": 1, "B": 2, "C": 3}
b = ["X", "Y", "Z"]

tmpC = []

keyL = []
valueL = []
#  - - - - - - - - - - -formdata - - - - - - - - - - - - - -

for i in a.keys():
    formdataD = {}
    formdataD["key"] = i
    keyL.append(i)
    for j in b:
        # formdataD = {}
        c = copy.deepcopy(a)
        c[i] = j
        tmpC.append(c)
        itemD["request"] = requestD
        itemL.append(itemD)
        # value在这里 key value type 其实都需要根据传参来改变的 ,type相对来说就是text
        formdataD["value"] = c[i]
        formdataD["type"] = "text"
    formdataL.append(formdataD)

#  - - - - - - - - - - -data - - - - - - - - - - - - - -
data["info"] = info
data["item"] = itemL

#  - - - - - - - - - - -url - - - - - - - - - - - - - -
urlD["raw"] = requestUrl
urlD["protocol"] = "https"
urlD["host"] = requestHost

#  - - - - - - - - - - -body - - - - - - - - - - - - - -
bodyD["mode"] = bodyMode
bodyD["formdata"] = formdataL

#  - - - - - - - - - - -itemD - - - - - - - - - - - - - -
itemD["name"] = "form -data类型"
itemD["protocolProfileBehavior"] = disabledSystemHeadersD
disabledSystemHeadersD["accept -encoding"] = "true"
disabledSystemHeadersD["connection"] = "true"
disabledSystemHeadersD["content -type"] = "true"
disabledSystemHeadersD["user -agent"] = "true"

#  - - - - - - - - - - -responseL - - - - - - - - - - - - - -
# response is python List,it's null
print(json.dumps(data, ensure_ascii=False))

接口自动化.zip

4.17 KB, 下载次数: 26, 下载积分: 吾爱币 -1 CB

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

518 发表于 2021-2-9 19:00

直接修改data可以不
比如

data['item'][0]['request']['body']['formdata'][0]['value']
学着用 发表于 2021-2-13 20:52
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 19:55

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表